0

我有一个 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;
}];
4

1 回答 1

1

restkit文档说,如果您使用loadObjectsAtResourcePath:usingBlock:,您可以从 a 块内部设置 objectMapping。

- (void)loadObjectUsingBlockExample {
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/monkeys.json" usingBlock:^(RKObjectLoader *loader) {
        loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[Monkey class]];
    }];
}
于 2012-10-07T04:03:49.593 回答