Display a nested array using RestKit / iOS

I need to display a nested array with RestKit, and I almost got it (I think).

JSON I need to map, looks like this

[ { _id: "5058670183970a0002000450", app_store_url: "http://itunes.apple.com/app/culturonda-alto-adige-sudtirol/id534467629", comments: [ { _id: "5058670783970a0002000456", }, { _id: "50588d7f83970a000200065c", } ] } ] 

My controllers for storing information are as follows:

 #import <Foundation/Foundation.h> #import "CommentData.h" @interface DesignData : NSObject @property (retain, nonatomic) NSString *designId; @property (retain, nonatomic) NSSet *commentsRelationship; @end 

and

 #import <Foundation/Foundation.h> @interface CommentData : NSObject @property (retain, nonatomic) NSString *commentId; @end 

My mapping is as follows:

 RKObjectManager *objectManager = [RKObjectManager sharedManager]; RKObjectMapping *commentsMapping = [RKObjectMapping mappingForClass:[CommentData class]]; [commentsMapping mapKeyPathsToAttributes:@"_id", @"commentId", nil]; RKObjectMapping *designMapping = [RKObjectMapping mappingForClass:[DesignData class] ]; [designMapping mapKeyPathsToAttributes:@"_id", @"designId", nil]; [designMapping mapKeyPath:@"comments" toRelationship:@"commentsRelationship" withMapping:commentsMapping]; [objectManager.mappingProvider setMapping:designMapping forKeyPath:@""]; 

Comments fall into the array, but are not stored in the comment controller. If I show the comments in the objecloader callback as follows:

 DesignData *designData = [objects objectAtIndex:0]; NSLog(@"Loaded %@ ", designData.commentsRelationship); 

this gives me:

 2012-10-20 22:37:55.258 RestKitTest5[4144:c07] Loaded {( <CommentData: 0x8689730>, <CommentData: 0x868bed0>, <CommentData: 0x868bfe0>, <CommentData: 0x868bf50> )} 

This is the beginning. But I can not:

 DesignData *designData = [objects objectAtIndex:0]; NSLog(@"Loaded %@ ", designData.commentsRelationship.commentId); 

So how can I store all this as an array?

+4
source share
2 answers

I already did everything right, I was just stupid in the end.

I got an array, and the objects in the array had the correct view (CommentData), so all I had to do was iterate over the array, and from there I could work with each object separately.

+1
source

I worked for me after I replaced this line:

 [designMapping mapKeyPath:@"comments" toRelationship:@"commentsRelationship" withMapping:commentsMapping]; 

WITH

 [designMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"comments" toKeyPath:@"commentsRelationship" withMapping: commentsMapping]]; 
+2
source

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


All Articles