我正在尝试在 iOS 中实现我自己的 Api,但我遇到了一个回调问题。
我已经用选择器实现了我的回调,但是当给定的函数在另一个文件/类中时,应用程序崩溃了。
这是错误:
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'
这是我的代码:
调用:(文件控制器.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)];
}
方法:(文件 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");
}