Reputation: 964
I have two classes that define Video and Cue objects. I get my data through an JSON API. The backend is written in Rails and has a one-to-many relationship between Video and Cues.
The data I get is structured like this:
{ "true":
[
{
"id" : 3,
"title" : "My Title"
[
{ "cues":
[
{
"id": 117,
"time" : "12.81",
"video_id" : 3
},
{
"id": 118,
"time" : "14.34",
"video_id" : 3
}
]
}
]
}
]
}
I have a method in Video.m that gets the JSON array, puts it in a dictionary and converts the dictionary to an array of Video objects.
+(id) getVideos {
NSURL *videoURL = [NSURL URLWithString:@"http://localhost:3000/myEndPoint"];
NSData *jsonData = [NSData dataWithContentsOfURL:videoURL];
NSError *error = nil;
NSDictionary *videoDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSMutableArray *videos = [NSMutableArray array];
for (NSDictionary *dict in videoDictionary[@"true"]) {
NSString *title = [dict objectForKey:@"title"];
Video *newVideo = [[Video alloc] initWithTitle:title];
[videos addObject: newVideo];
}
return videos;
}
How should I format the cues so that I can get specific cues from a Video, like in a relational sort of way if possible, if not then just the best practise. In Rails it would be video.cues. Can I do this in Objective C?
Would be perfect if I could end up having the ability to do something like:
Cue *currentCue = video.cues[0];
Upvotes: 0
Views: 67
Reputation: 20692
Add a property to your Video class: NSArray *cues
. And change your init method to initWithTitle:(NSString*)title andCues:(NSArray*)cues
. That way you'll be able to do exactly that: video.cues[0]
Upvotes: 1