0

我正在尝试在 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");
}
4

1 回答 1

1

在特定类型的实例上执行选择器。您需要传递将选择器消息发送给它的对象以及选择器。

在上述情况下,您在 Controller.m 中有一个选择器,aaa它可能是 的实例上的有效方法Controller,但在 Api.m 文件中,您试图aaa在 的实例上调用该方法Api,这会导致崩溃因为该方法对该类无效。

于 2013-09-18T19:44:21.990 回答