Restkit Mapping Nested Array
I got a question for mapping a nested array. On https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md there's an example for mapping nested objects.
But what do I have to do, if the nested object is an array of objects, for example the JSON looks like:
{ "articles": [
{ "title": "RestKit Object Mapping Intro",
"body": "This article details how to use RestKit object mapping...",
"author": [{
"name": "Blake Watters",
"email": "blake@restkit.org"
},
{
"name": "abc",
"email": "emailaddress"
}]
"publication_date": "7/4/2011"
}]
}
How should my classes look like, for getting an array of Authors? Thats the code from the example:
@interface Author : NSObject
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSString* email;
@end
@interface Article : NSObject
@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) NSString* body;
@property (nonatomic, retain) Author* author; // should be an array of Author-Objects
@property (nonatomic, retain) NSDate* publicationDate;
@end
How can I tell Restkit, that theres an Array of Authors and if I am changing the class of the author Attribute in Article to NSArray, how does Restkit knows, that in this NSArray should be Author-Objects...??
I am using RKObjectMapping to map Objects from JSON to Objective-c and vice versa.
You'll want to make sure you set the var type accordingly:
@interface Article : NSObject
@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) NSString* body;
@property (nonatomic, retain) NSSet* authors;
@property (nonatomic, retain) NSDate* publicationDate;
@end
It may work with declaring it as an NSArray but I personally use RestKit with CoreData models and the relations in those scenarios are NSSet's.
Additionally, you need to set up the mapping:
[articleMapping mapKeyPath:@"author" toRelationship:@"authors" withMapping:authorMapping];
将其分割出来有所帮助。
@interface Author : NSObject
@property (nonatomic, retain) NSString* name;
@property (nonatomic, retain) NSString* email;
@end
@interface Article : NSObject
@property (nonatomic, retain) NSString* title;
@property (nonatomic, retain) NSString* body;
@property (nonatomic, retain) NSArray* author;
@property (nonatomic, retain) NSDate* publicationDate;
@end
//create the article mapping
RKObjectMapping *articleMapping = [RKObjectMapping mappingForClass:[Article class]];
//add rest of mappings here
[articleMapping addAttributeMappingsFromDictionary:@{
@"title":@"title"
}
//create the author mapping
RKObjectMapping *authorMapping = [RKObjectMapping mappingForClass:[Author class]];
//add rest of mappings here
[authorMapping addAttributeMappingsFromDictionary:@{
@"name":@"name"
}
//link mapping with a relationship
RKRelationshipMapping *rel = [RKRelationshipMapping relationshipMappingFromKeyPath:@"authors" toKeyPath:@"author" withMapping:authorMapping];
//add relationship mapping to article
[articleMapping addPropertyMapping:rel];
整个respnse字符串将其作为nsmutable字典,然后将作者值分配给namutable数组。
链接地址: http://www.djcxy.com/p/49190.html上一篇: 使用Restkit为NSManagedObject类创建实例
下一篇: Restkit映射嵌套数组