RestKit non-kvc object mapping

2019-04-15 22:35发布

问题:

I am experimenting with integrating RestKit into my current project. I am retrieving data with the format:

[
    {"id":"1",
    "name":"john"}
    {"id":"2",
    "name":"bob"}
]

Following the excellent tutorial in the RestKit wiki, I see that the recommended, fully KVC-compliant format would be:

{"customers": [
    {"id":"1",
    "name":"john"}
    {"id":"2",
    "name":"bob"} ]
}

Then I could use

[mappingProvider setMapping:customerMapping forKeyPath:@"customers"];

Unfortunately, I do not at the moment have control over the data format on the server.

Later in the RestKit wiki, there is a section that solves my problem: "Mapping without KVC." I would associate the objectMapping with the objects at load-time.

[objectManager loadObjectsAtResourcePath:@"/data" objectMapping:customerMapping delegate:self];

Here's my problem: The wiki is for 0.9.3. But this method is deprecated in 0.10.1. Yes, it still works - for now.

If this method is being deprecated, and keeping in mind that I don't control the data, what is the appropriate way to assign an object mapping to a class in this sort of non-KVC situation?

回答1:

I am not positive about this but I believe it is deprecated in support of blocks... this might work:

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/data" usingBlock:^(RKObjectLoader* loader) {
    [loader setObjectMapping:customerMapping];
}];

And, naturally, use the block to replace the delegate methods.