I’m having a problem writing a class method wich have a method has argument.
The function is inside the class “SystemClass.m/h”
//JSON CALL
+(void)callLink:(NSString*)url toFunction:(SEL)method withVars:(NSMutableArray*)arguments {
if([self checkConnection])
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *datas = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
[arguments addObject:datas];
[self performSelectorOnMainThread:@selector(method:) withObject:arguments waitUntilDone:YES];
});
}else{
[self alertThis:@"There is no connection" with:nil];
}
}
What the function does is to call a JSON url, and give data to a Method
I use it like this:
[SystemClass callLink:@"http://www.mywebsite.com/call.php" toFunction:@selector(fetchedInfo:) withVars:nil];
but it crashes like this:
Terminating app due to uncaught exception
‘NSInvalidArgumentException’, reason: ‘+[SystemClass method:]:
unrecognized selector sent to class 0x92d50’
May you help me please? I’m trying to found the solution anyway!
Thanks, Alex
In you callLink method you already give a selector as argument (it’s the argument called “method”). Moreover you need to add one more argument, because the “method” argument should be invoked from an object that implement this method (and in the example you give us, the applicaiton will try to call the method named “method” from SystemClass when you call :
Here self is the SystemClass and a such method doesn’t seem to exist in SystemClass, that’s why it is crashing). So add a target (an id object) to the arguments :
So for the following line you should just give the selector and call this selector on the target object :
And not :
Improvement :
Before calling the selector you should check if the target responds to the selector doing something like that (it’ll prevent your application from crashing). Instead of doing this :
Do this :