我有一个API,我正在查询当我查询组织列表时,我得到了一个数组:
{
id: integer,
name: string
}
但我也能够获得每个返回单个对象的组织的详细信息,如:
{
id: integer,
name: string,
description: text,
visit_count: integer,
image_url: string
}
我如何在RestKit中设置对象?
答案 0 :(得分:1)
您需要设置以下内容:
对象管理器
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:YOUR HOST NAME]];
objectManager.requestSerializationMIMEType=RKMIMETypeJSON;
包含对象中所有元素的映射对象
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[YOUR CLASS class]];
对象的映射
[mapping addAttributeMappingsFromArray:@[@"id",
@"name",
@"description",
@"visit_count",
@"image_url"
]];
响应描述符
RKResponseDescriptor *responseDesciptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:OBJECT URL keyPath:YOUR KEY PATH statusCodes:nil];
将响应描述符添加到对象管理器
[objectManager responseDesciptor];
现在你可以点击服务器了
[[RKObjectManager sharedManager] postObject:nil path:OBJECT URL parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
self.variable = [NSMutableArray arrayWithArray: mappingResult.array];
}
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An Error Has Occurred" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}];