Reputation: 4092
just trying to get used to restkit for iphone apps here at the moment and ive hit a wall. Im trying to get data from a nested json file.
[
{"person": {
"name": "joe",
"id": "1234",
"numbers":
[
{"value": "555-12125"},
{"value": "222-12125"}
]
}
}]
i set up the rkobjectmanager like so
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000"];
[manager loadObjectsAtResourcePath:@"/J.json?user_id=36995582&planner=insta-grammar" objectClass:[Person class] delegate:self] ;
[manager registerClass:[Person class] forElementNamed:@"person"];
[manager registerClass:[Numbers class] forElementNamed:@"numbers"];
then the person class and numbers class as follows
#import "Person.h"
@implementation Person
@synthesize _name,_id,_number;
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"name",@"name",
@"id",@"id",nil];
}
+ (NSDictionary*)elementToRelationshipMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"numbers", @"numbers",
nil];
}
@end
#import "Numbers.h"
@implementation Numbers
@synthesize _number;
+ (NSDictionary*)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"value",@"value",nil];
}
+ (NSString*)primaryKeyProperty {
return @"value";
}
+ (NSDictionary*)relationshipToPrimaryKeyPropertyMappings {
return [NSDictionary dictionaryWithObject:@"value" forKey:@"value"];
}
@end
but every time the _number remains empty after the didloadobjects function is called while the name and id variables fill up fine. any ideas on this?. ive tried the example on git but couldnt get it to work so any help would be appreciated. thanks g
Upvotes: 1
Views: 1780
Reputation: 4092
Just for anyone following up on this. Finally got this all working using the new OM2 that they have just released with restkit. very nice and easy to use change
Upvotes: 0
Reputation: 3772
What you need to do is to declare an NSArray *_numbers
member variable in Person.h, make it a property like @property (nonatomic, retain) NSArray *_numbers
and synthesize it in Person.m like @synthesize numbers = _numbers
. Then you can get rid of whatever _number is, since you won't be storing a single number object, you will be storing an array of number objects.
Your Numbers class should instead just be Number, and you should synthesize your number variable like @synthesize number = _number;
. So your Number.m should look like
@implementation Number
@synthesize number = _number;
+ (NSDictionary *)elementToPropertyMappings {
return [NSDictionary dictionaryWithKeysAndObjects:
@"value", @"number", nil];
}
Because your property is number, the mapping dictionary value is @"number"
, while your source JSON key should be @"value"
.
Then you can load the objects like (note we are using our Number
class for the @"numbers"
element):
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000"];
[manager registerClass:[Person class] forElementNamed:@"person"];
[manager registerClass:[Number class] forElementNamed:@"numbers"];
[manager loadObjectsAtResourcePath:@"/J.json?user_id=36995582&planner=insta-grammar" delegate:self];
For a full example see the Catalog Example int the RestKit github project
Upvotes: 3