I try to map the response xml into object and the xml looks like:
<list version="1.0">
<meta>
<type>resource-list</type>
</meta>
<resources start="0" count="168">
<resource classname="Quote">
<field name="name">Alpha</field>
<field name="price">10</field>
</resource>
<resource classname="Quote">
<field name="name">Beta</field>
<field name="price">9</field>
</resource>
</resources>
</list>
What I want is resources
part. I create the object model:
@interface Field : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSNumber *price;
@end
@interface Resources : NSObject
@property (nonatomic, strong) NSNumber *count;
@property (nonatomic, strong) NSArray *resource;
@end
The problem is : I don't know how to set the mapping for resource
of Resources
. I once tried using mapping:
RKObjectMapping *objMapping = [RKObjectMapping mappingForClass:[Field class]];
[objMapping addAttributeMappingsFromArray:@[@"name",@"price"]];</br>
RKObjectMapping *resourcesMapping = [RKObjectMapping mappingForClass:[Resources class]];
[resourcesMapping addAttributeMappingsFromArray:@[@"count"]];
[resourcesMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"resource" toKeyPath:nil withMapping:objMapping]];
RKResponseDescriptor *descriptor = [RKResponseDescriptor responseDescriptorWithMapping:resourcesMapping method:RKRequestMethodGET pathPattern:@"XXXXXX" keyPath:@"list.resources" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
After the mapping is finished, the Resources
object is created. However the array resource
of it is still nil.
How can I get the array of resource in the xml?
---Updated-----
I updated the code, which is
RKObjectMapping *objMapping = [RKObjectMapping mappingForClass:[Field class]];
objMapping.forceCollectionMapping = YES;
[objMapping addAttributeMappingsFromDictionary:@{@"name":@"name.text"}];
I also update the respondscripter with keypath:
RKResponseDescriptor *descriptor = [RKResponseDescriptor responseDescriptorWithMapping:objMapping method:RKRequestMethodGET pathPattern:@"XXXXX" keyPath:@"list.resources.resource.field" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
Then in the success block of RKObjectManager
, the mapping results have the field array. The problem is the Field
's property are all nil
.
I check the log which looks:
D restkit.object_mapping:RKMapperOperation.m:229 Asked to map source object (
{
name = name;
text = "alpha";
},
{
name = price;
text = "98.3";
}
) with mapping name.text>"
)>
2014-05-14 23:33:57.681 RestKitTest[5138:3307] D restkit.object_mapping:RKMappingOperation.m:859 Starting mapping operation...
2014-05-14 23:33:57.682 RestKitTest[5138:3307] W restkit.object_mapping:RKMappingOperation.m:338 Destination object rejected attribute value (
name,
price
) for keyPath name.text. Skipping...
It seems that the attribute of the xml becomes a value of the map source object. How I set the configuration to parse this kind of object?