Mapping arrays in restkit
In objective-c with Restkit.
I'm trying to mapping an object like this:
{
"key1":"key1",
"key2":"key2",
"key3":[{
"otherkey1":"otherkey1",
"otherkey2":"otherkey2"
},
{
"otherkey1":"otherkey1",
"otherkey2":"otherkey2"
},
]
}
and I'm doing something like this:
RKObjectManager *objMan = [[RKObjectManager alloc] initWithHTTPClient: client];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[@"key1",
@"key2",
@"key3"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[objContainer class] rootKeyPath:nil method:RKRequestMethodAny];
/**/
RKObjectMapping *requestMapping1 = [RKObjectMapping requestMapping];
[requestMapping1 addAttributeMappingsFromArray:@[@"otherkey1",
@"otherkey2",
]];
RKRequestDescriptor *requestDescriptor1 = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping1 objectClass:[ContainerClient class] rootKeyPath:@"key3" method:RKRequestMethodAny];
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager addRequestDescriptor:requestDescriptor];
RKObjectMapping *objMapping = [RKObjectMapping mappingForClass:[objContainer class]];
[objMapping addAttributeMappingsFromDictionary:@{
@"key1" : @"key1",
@"key2" : @"key2",
@"key3" : @"key3",
}];
/**/
RKObjectManager *manager1 = [RKObjectManager sharedManager];
[manager1 addRequestDescriptor:requestDescriptor1];
RKObjectMapping *objMapping1 = [RKObjectMapping mappingForClass:[ContainerClient class]];
[objMapping1 addAttributeMappingsFromDictionary:@{
@"otherkey1" : @"otherkey1",
@"otherkey2" : @"otherkey2",
}];
/**/
Classe ContainerClient
@property (strong, nonatomic) NSNumber* otherkey1;
@property (strong, nonatomic) NSString *otherkey2;
Class objContainer
@property (strong, nonatomic) NSNumber* key1;
@property (strong, nonatomic) NSNumber* key2;
@property (strong, nonatomic) NSMutableArray* key3;
have something like this code to communicate with the server when it sends the objects do not have lists and works. how do I map a list?
You need to set up response descriptors - these are totally missing at the moment.
If key3
is supposed to contain instances of ContainerClient
then you will need a relationship mapping too. Do this using addRelationshipMappingWithSourceKeyPath:mapping:
.
上一篇: iOS Restkit:如何解析嵌套路径
下一篇: 在restkit中映射数组