响应 RestKit 附加数据 [英] RestKit additional data in response
问题描述
我正在使用 RestKit 将数据从我的 api 映射到 CoreData 实体,我想知道如何从响应中获取额外的数据.例如我的 api 返回结构如:
I am using RestKit for mapping data from my api to CoreData entities and i wonder how can i get additional data from response. For example my api return structure like:
{
"items": [
{
"id": 1,
"title": "Title"
},
{
"id": 2,
"title": "Title 2"
}
],
"someParameter": "someValue"
}
我已经有了共享对象管理器的正确映射,所以我只发送请求:
i already have right mappings for shared object manager so i just send request:
[[RKObjectManager sharedManager] getObjectsAtPath:@"_api/items"
parameters:parameters
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
//handle success
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
//handle error
}];
如何在 success
块中获取 someParameter
值?这可能吗?
How can i get someParameter
value in success
block? Is this possible?
推荐答案
您需要稍微调整映射.如果您按如下方式更改它,您应该能够让 RESTkit 为您解析someParameter"属性.您需要有两个类(父类和子类).
You will need to tweak your mapping slightly. If you changed it as follows you should be able to get RESTkit to parse the 'someParameter' attribute for you. You need to have two classes (Parent and Child).
Parent 类有 2 个属性(someParameter 和一个 Child 对象数组).addRelationshipMappingWithSourceKeyPath 将父对象和子对象映射联系在一起.
The Parent class has 2 attributes (someParameter and an array of Child objects). The addRelationshipMappingWithSourceKeyPath is what ties the Parent and Child object mappings together.
代码:
RKObjectMapping *parentMapping = [RKObjectMapping mappingForClass:[Parent class]];
[beaconActionMapping addAttributeMappingsFromDictionary:@{
@"someParameter" : @"someParameter"
}];
RKObjectMapping *childMapping = [RKObjectMapping mappingForClass:[Child class]];
[beaconMapping addAttributeMappingsFromDictionary:@{
@"id" : @"childId",
@"title" : @"title"
}];
[parentMapping addRelationshipMappingWithSourceKeyPath:@"items" mapping:childMapping];
类层次结构:
@interface Parent : NSObject
@property(nonatomic,strong) NSString *someParameter;
@property(nonatomic,strong) NSArray *items; // Array of Child objects
@end
@interface Child : NSObject
@property(nonatomic,strong) NSString *childId;
@property(nonatomic,strong) NSString *title
@end
这篇关于响应 RestKit 附加数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!