0

我正在发送一个包裹在铁 mq 消息中的 json blob。

对于 Restkit,它是:

{
id:"2837409187409328",
delay:60,
body:"{ myJson:{ "hey":true}}"
}

我使用 rkrelationship 将子对象与主体映射:作为 CustomObject 类型。

但是,当 Restkit 尝试映射到该自定义对象时,它会爆炸,因为它将“body”视为 NSString 而不是 NSDictionary,并尝试使用 sourceKeyPath 从结果 sourceObject 中获取值......但由于它是 NSString 它炸毁。和:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7fcf91165060> valueForUndefinedKey:]: this class is not key value coding-compliant for the key alreadyLiked.'

我尝试使用动态映射并用 NSDictionary 替换表示。

我已尝试按照文档中的建议对访问器进行验证,但从未调用过该代码。有任何想法吗?

4

1 回答 1

2

通过后期处理解决。我最终获取了传递给主体的字符串,覆盖了该主体属性的设置器,并在该设置器中运行映射操作以获取 json 字符串,并将其映射到我的自定义对象(在转换为字典之后)然后将其设置在我的父对象上。

希望这对你有帮助!

//here I have an object with a string property called Body that contains JSON.
//I extract the json, turn it into a dictionary then map it to another property on that same object that actually has a relationship mapping...
//always check to see if the body actually exists before you try to map it 
//otherwise you will have a crash in your overridden setter..
if(self.Body){
        NSData *data = [self.Body dataUsingEncoding:NSUTF8StringEncoding];
        id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        NSDictionary *representation = json;
        //NSDictionary *metadata = @{ @"URL": [NSURL URLWithString:@"http://restkit.org"]http://restkit.org"] };

        RKObjectMapping *objectMapping = [MyObject mapping];

        RKMappingOperation *operation = [[RKMappingOperation alloc] initWithSourceObject:representation destinationObject:mappedResultDTO mapping:objectMapping];

        RKObjectMappingOperationDataSource *dataSource = [RKObjectMappingOperationDataSource new];
        operation.dataSource = dataSource;
        // [operation start];

        NSError *error = nil;
        BOOL success = [operation performMapping:&error];
    }
于 2015-01-18T20:35:05.583 回答