Mapping a Restkit Nested Array

I have a question about displaying a nested array. At https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md there is an example for matching nested objects.

But what should I do if the nested object is an array of objects, for example JSON looks like this:

{ "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" }] } 

What should my classes look like to get an array of authors? This is 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 Array of Authors and if I change the author attribute class in an NSArray article, how does Restkit know that there must be Author-Objects in this NSArray ... ??

I use RKObjectMapping to map objects from JSON to Objective-c and vice versa.

+6
source share
4 answers

You must make sure that you specify 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 can work with declaring it as NSArray, but I personally use RestKit with CoreData models, and the relationship in these scenarios is NSSet.

In addition, you need to configure the display:

 [articleMapping mapKeyPath:@"author" toRelationship:@"authors" withMapping:authorMapping]; 
+7
source

Separation helps.

 @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]; 
+3
source

The entire respnse line treats it as an nsmutable dictionary, then you assign the values โ€‹โ€‹of the author to an arbitrary array.

0
source

What you need to do is use the NSSet property to store your nested array. Here is a complete guide that will certainly help you do this https://medium.com/ios-os-x-development/restkit-tutorial-how-to-fetch-data-from-an-api-into-core- data-9326af750e10 .

0
source

Source: https://habr.com/ru/post/914017/


All Articles