6

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": "[email protected]"
      },
      {
          "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.

4 Answers 4

8

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];
Sign up to request clarification or add additional context in comments.

Comments

3

Splitting it out 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];

Comments

0

The Whole respnse string take it as nsmutable dictionary then you assign author values to namutable array..

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.