0

我正在尝试发布一个带有块的对象。OnDidFailWithError从来没有被叫过,它爆炸了。

这是我得到的错误:

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'+ [MyClass objectLoader:didFailWithError:]:无法识别的选择器发送到类

这是我的代码:

[[RKObjectManager sharedManager] postObject: myObj usingBlock:^(RKObjectLoader *loader){
    loader.targetObject = nil;
    loader.delegate = (id)self;
    loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[MyClass class]];

    loader.onDidFailWithError = ^(NSError *error) {
        NSLog(@"Error %@", [error localizedDescription]);
    };

    loader.onDidLoadObject = ^(id obj) {
        NSLog(@"Comment");
        NSLog(@"%@",obj);
    };

    loader.onDidLoadResponse = ^(RKResponse *response) {
        NSLog(@"Response: %@", [response bodyAsString]);
    };

    loader.onDidLoadObjects=^(NSArray* objects){
        //post notification
        [[NSNotificationCenter defaultCenter] postNotificationName:@"finish" object:nil];
    };

    loader.serializationMIMEType = RKMIMETypeJSON; // We want to send this request as JSON
    loader.method = RKRequestMethodPOST;

    loader.serializationMapping = [RKObjectMapping serializationMappingUsingBlock:^(RKObjectMapping* mapping) {
        [mapping mapAttributes:@"field1", @"field2",nil];
    }];
}];
4

2 回答 2

0

您正在将委托设置为 self,这将在您当前的控制器上调用那些 RestKit 委托方法,而不是使用您创建的块。

于 2012-12-04T17:22:49.893 回答
0

如果您无法处理错误,请在onDidLoadResponse上尝试此操作

    loader.onDidLoadResponse = ^(RKResponse *response) {

        NSLog(@"Response did arrive");
        if([response statusCode]>299){

            NSError *error = nil;
            id parsedResponse = [NSJSONSerialization JSONObjectWithData:[response body] options:NSJSONWritingPrettyPrinted error:&error];
            NSLog(@"%@",parsedResponse);

        }

    };
于 2012-09-25T19:15:06.337 回答