title: Mapping nested objects with RestKit in Xcode using Objective-C
body: <p>With the help of mja, I managed to successfully set up simple object mapping using RestKit and Objective-C. Please see my previous question <a href="https://stackoverflow.com/questions/8309543/mapping-a-json-response-to-an-object-using-restkit-and-objective-c">here</a>.</p>
  
  <p>My next step was to attempt to deal with nested JSON in the same way.</p>
  
  <p>My JSON looks like this, with the outer being a CandidatePhrase with some nested 'Votes':</p>
  
  <pre><code>{ "Id":33696,
  "Phrase": "phrase",
  "BadCount":0,
  "Votes":[{"Id":447,"OriginalId":33696,"Votes":2,"Translation":"translation 1"},
  {"Id":746,"OriginalId":33696,"Votes":1,"Translation":"translation 2"},
  {"Id":747,"OriginalId":33696,"Votes":1,"Translation":"translation 3"}
  ]}
  </code></pre>
  
  <p>I created a relationship in my AppDelegate as follows:</p>
  
  <pre><code>[candidatePhraseMapping mapKeyPath:@"votes" toRelationship:@"vote" withMapping:voteMapping];
  </code></pre>
  
  <p>When I call make my request in my controller, I'm able to deal with the rest of the CandidatePhrase okay, but am not really sure how to map the nested 'Vote' objects into an array so I can use them in a TableView</p>
  
  <p>(pseudo-code something like this...)</p>
  
  <pre><code>// Store the votes in an array
  _votes = [[NSArray alloc] initWithObjects:myCandidatePhrase.votes, nil];
  </code></pre>
  
  <p><strong>Here's my CandidatePhrase Object</strong></p>
  
  <pre><code>@interface CandidatePhrase : NSObject
  
  @property (nonatomic, retain) NSNumber* ident;
  @property (nonatomic, retain) NSNumber* badcount;
  @property (nonatomic, retain) NSString* phrase;
  @property (nonatomic, retain) NSArray* votes;
  
  @end
  </code></pre>
  
  <p><strong>and my Vote object</strong></p>
  
  <pre><code>@interface Vote : NSObject
  
  @property (nonatomic, retain) NSNumber* ident;
  @property (nonatomic, retain) NSNumber* originalId;
  @property (nonatomic, retain) NSNumber* votecount;
  @property (nonatomic, retain) NSString* translation;
  
  + (id)voteWithTranslationId:(NSNumber *)ident translation:(NSString *)translation;
  
  @end
  </code></pre>
  
  <p>Any help would be much appreciated.</p>
  
  <h1>EDIT</h1>
  
  <p>Below is my mapping code</p>
  
  <pre><code>// Votes Mapping
  
  RKObjectMapping* voteMapping = [RKObjectMapping mappingForClass:[Vote class]];
  [voteMapping mapKeyPath:@"Id" toAttribute:@"ident"];
  [voteMapping mapKeyPath:@"OriginalId" toAttribute:@"originalId"];
  [voteMapping mapKeyPath:@"Votes" toAttribute:@"votecount"];
  [voteMapping mapKeyPath:@"Translation" toAttribute:@"translation"];
  
  [[manager mappingProvider] addObjectMapping:voteMapping];
  
  // Candidate Phrase Mapping
  RKObjectMapping *candidatePhraseMapping = [RKObjectMapping mappingForClass:[CandidatePhrase class]];
  
  [candidatePhraseMapping mapKeyPath:@"Id" toAttribute:@"ident"];
  [candidatePhraseMapping mapKeyPath:@"Phrase" toAttribute:@"phrase"];
  [candidatePhraseMapping mapKeyPath:@"BadCount" toAttribute:@"badcount"];
  
  [candidatePhraseMapping mapKeyPath:@"Votes" toRelationship:@"votes" withMapping:voteMapping];
  
  [[manager mappingProvider] addObjectMapping:candidatePhraseMapping];
  </code></pre>
  
  <p>For clarity also, here's how I'm attempting to access the vote items on the controller</p>
  
  <pre><code>- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object 
  { 
      CandidatePhrase *myCandidatePhrase = (CandidatePhrase*)object;
      self.candidateText.text = myCandidatePhrase.phrase; &lt;-- works fine
  
      _votes = [[NSArray alloc] initWithObjects:myCandidatePhrase.votes, nil];
      for (id o2 in _votes) { 
          //Vote *vote = o2; 
          NSLog(@"Item name: %@", o2); &lt;-- sees object but crashes
      }
  
      NSLog(@"Votes: %@", myCandidatePhrase.votes); 
      _votes = [[NSArray alloc] initWithObjects:myCandidatePhrase.votes, nil];
       [_votesTableView reloadData];
   } 
  </code></pre>
  
  <p>and my table is binding with</p>
  
  <pre><code>Vote *vote = [_votes objectAtIndex:indexPath.row];
  cell.textLabel.text = vote.translation;
  </code></pre>