Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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": "[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.

like image 704
android Avatar asked Apr 24 '12 09:04

android


2 Answers

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];
like image 182
shawnwall Avatar answered Sep 20 '22 12:09

shawnwall


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];
like image 41
Kalel Wade Avatar answered Sep 21 '22 12:09

Kalel Wade