Question

I´m having some problems with performSelector method on this code:

This method are in other class called "JSONMethods":

+(void)sendPostMsgWithMultipleArguments:(NSArray *)myArgs {
[self sendPostMsg:[myArgs objectAtIndex:0]:[myArgs objectAtIndex:1]];
}

Then, on another class I have the call:

- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Loading...";

JSONMethods *methods = [[JSONMethods alloc]init];
NSArray *arguments = [[NSMutableArray alloc]initWithObjects:@"http://localhost/promos/txFirmas.php",[NSString stringWithFormat:@"sensor=%d",tableViewNumber], nil];
[methods performSelector:@selector(sendPostMsgWithMultipleArguments:)
           withObject:arguments
           afterDelay:3.0];

NSString *tit = [NSString stringWithFormat:@"Sign: %d",tableViewNumber];
self.title = tit;
}

I would like wait until the "sendPostMsgWithMultipleArguments:" finish for change the title, how I can make it? When I test this, mi app crash and show me this by console:

2012-08-17 12:09:15.966 MapaProject[524:11603] -[JSONMethods sendPostMsgWithMultipleArguments:]: unrecognized selector sent to instance 0x7c85b70
2012-08-17 12:09:15.968 MapaProject[524:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[JSONMethods sendPostMsgWithMultipleArguments:]: unrecognized selector sent to instance 0x7c85b70'
*** First throw call stack:
(0x133a022 0x2016cd6 0x133bcbd 0x12a0ed0 0x12a0cb2 0xa1c85d 0x130e936 0x130e3d7 0x1271790 0x1270d84 0x1270c9b 0x15db7d8 0x15db88a 0xf6626 0x236d 0x20c5)
terminate called throwing an exception

Can you help me? Thanks in advance :)

Was it helpful?

Solution

Change +(void)sendPostMsgWithMultipleArguments:(NSArray *)myArgs

to -(void)sendPostMsgWithMultipleArguments:(NSArray *)myArgs

or even better change

[methods performSelector:@selector(sendPostMsgWithMultipleArguments:) withObject:arguments afterDelay:3.0];

to [[methods class] performSelector:@selector(sendPostMsgWithMultipleArguments:) withObject:arguments afterDelay:3.0];

OTHER TIPS

NSArray * mutArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:0],[NSNumber numberWithInt:2], nil]

[self performSelector:@selector(loadMore:) withObject:mutArray afterDelay:1.9];

-(void)loadMore:(NSArray *)array
{
 NSNumber * myNumber = [array objectAtIndex:0];
 NSNumber * myNumber2 = [array objectAtIndex:1];

}

In swift 3

You can make dictionary to save multiple arguments and pass it with the selector

let argumentsDict = ["arg1Key": arg1Value, "arg2Key": arg2Value] as [String : Any]

perform(#selector(selectorWithMultipleArg), with: argumentsDict, afterDelay: 3.0)

And inside the selector function, take out values using guard and use them. I am using 2 different type arguments to explain better .i.e. Int and String

func selectorWithMultipleArg(_ arguments: [String: Any]) {

    guard let arg1Value = arguments["arg1Key"] as? Int else {
        fatalError("Invalid value 1")
    }

    guard let arg2Value = arguments["arg2Key"] as? String else {
        fatalError("Invalid value 2")
    }
    print("Value 1: \(arg1Value)")
    print("Value 2: \(arg2Value)")

}

I hope this would help you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top