不推荐使用的设置对象映射的方法

时间:2012-10-07 01:15:00

标签: objective-c ios restkit

我运行的Ruby On Rails本地服务器为我提供了以下JSON:

[ { "content" : "Here is content",
    "created_at" : "2012-10-07T01:06:51Z",
    "id" : 4,
    "name" : "Joe",
    "title" : "Title 1",
    "updated_at" : "2012-10-07T01:06:51Z"
  },
  { "content" : "Here is content",
    "created_at" : "2012-10-07T01:07:09Z",
    "id" : 5,
    "name" : "John",
    "title" : "Title2",
    "updated_at" : "2012-10-07T01:07:09Z"
  }
]

我正在使用RestKit将其解析为Post模型对象,如下所示:

- (void)loadObjects
{
    RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:[Post class]];
    [objectMapping mapKeyPath:@"id" toAttribute:@"postId"];
    [objectMapping mapKeyPath:@"created_at" toAttribute:@"createdAt"];
    [objectMapping mapKeyPath:@"content" toAttribute:@"content"];
    [objectMapping mapKeyPath:@"title" toAttribute:@"title"];
    [objectMapping mapKeyPath:@"name" toAttribute:@"name"];
    [objectMapping mapKeyPath:@"updated_at" toAttribute:@"updatedAt"];

    RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURLString:@"http://localhost:3000/"];
    [manager loadObjectsAtResourcePath:@"/posts" objectMapping:objectMapping delegate:self];
}

- (void)objectLoader:(RKObjectLoader *)objectLoader didLoadObjects:(NSArray *)objects {
    NSLog(@"%@", objects);
}

这很好用,但loadObjectsAtResourcePath:objectMapping:delegate:已弃用。我假设我应该使用loadObjectsAtResourcePath:delegate:,但我无法找到设置objectMapping的方法。有什么想法吗?

更新

我现在正在设置委托:

[manager loadObjectsAtResourcePath:@"/posts" usingBlock:^(RKObjectLoader *loader) {
    loader.objectMapping = objectMapping;
    loader.delegate = self;
}];

1 个答案:

答案 0 :(得分:1)

restkit documentation表示如果您使用loadObjectsAtResourcePath:usingBlock:,则可以从块内设置objectMapping。

- (void)loadObjectUsingBlockExample {
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/monkeys.json" usingBlock:^(RKObjectLoader *loader) {
        loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[Monkey class]];
    }];
}