Reputation: 3970
I'm trying to implement my own API in iOS but I have one problem with callbacks.
I have implemented my callbacks with selectors but, when the function given is in another file/class, the app crashes.
This is the error:
2013-09-18 21:32:16.278 Vuqio[6498:19703] -[VuqioApi postCurrenProgramRequestDidEnd]: unrecognized selector sent to instance 0xa5a2150
2013-09-18 21:32:16.278 Vuqio[6498:19703] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[VuqioApi postCurrenProgramRequestDidEnd]: unrecognized selector sent to instance 0xa5a2150'
An this my code:
Call: (File Controller.m)
...
[self softCheckIn:@"922337065874264868506e30fda-1c2a-40a5-944e-1a2a13e95e95" inProgram:p.idProgram callback:@selector(postCurrenProgramRequestDidEnd)];
...
-(void)postCurrenProgramRequestDidEnd
{
NSLog(@"Soft check-in");
}
- (void)softCheckIn:(NSString *)userId inProgram:(NSString *)program callback:(SEL)callback
{
// Hacemos un soft checkin
VuqioApi *api = [[VuqioApi alloc] init];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
userId, @"userId",
program, @"programId",
nil];
[api postCurrentProgram:data withSuccess:callback andFailure:@selector(aaa)];
}
Methods: (File Api.m)
- (void)postCurrentProgram:(NSDictionary *)data withSuccess:(SEL)successCallback
{
NSLog(@"Performing selector: %@", NSStringFromSelector(successCallback));
[self postCurrentProgram:data withSuccess:successCallback andFailure:@selector(defaultFailureCallback)];
}
- (void) postCurrentProgram:(NSDictionary *)data withSuccess:(SEL)successCallback andFailure:(SEL)failureCallback {
[self placePostRequest:@"api/programcurrent" withData:data withHandler:^(NSURLResponse *urlResponse, NSData *rawData, NSError *error) {
NSLog(@"Performing selector: %@", NSStringFromSelector(successCallback));
NSLog(@"Performing selector: %@", NSStringFromSelector(failureCallback));
NSString *string = [[NSString alloc] initWithData:rawData encoding:NSUTF8StringEncoding];
//NSDictionary *json = [NSJSONSerialization JSONObjectWithData:rawData options:0 error:nil];
if ( ![string isEqual: @"ok"])
{
[self performSelector:failureCallback withObject:self];
} else {
NSLog(@"OK");
[self performSelector:successCallback];
}
}];
}
- (void) defaultFailureCallback {
NSLog(@"Failure");
}
Upvotes: 0
Views: 149
Reputation: 69342
A selector is performed on an instance of a certain type. You need to pass the object that will have the selector messaged to it as well as the selector.
In the case above, you have a selector in Controller.m aaa
which might be a valid method on an instance of Controller
, but in the Api.m file, you're attempting to call the method aaa
on an instance of Api
, which results in a crash since that method isn't valid for that class.
Upvotes: 1