Restkit OM2 relationship mapping
Im using Restkit OM2 to take in a json and map to objects on iphone. Im currently confused on how to structure the mappings and could do with some help.
Below is an example json file
{
-magic_verbs: [
-{
lemma: "work"
position: 5
score: "0.75"
value: "working"
}
-{
lemma: "head"
position: 0
score: "0.75"
value: "heading"
}
],
magic_advs: [
-{
lemma: "not"
position: 2
score: "0.6"
value: "not"
}
-{
lemma: "just"
position: 2
score: "0.6"
value: "just"
}
]
}
i only need the lemma and value fields from each of these. so for example the verb class contains
@interface Verbs : NSManagedObject {
}
@property (nonatomic,retain) NSString *lemma;
@property (nonatomic,retain) NSString *value;
@end
@implementation Verbs
@synthesize lemma,value;
@end
then i read in the json and create the mappings with below code
objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://localhost:3000"];
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"RKRelationshipMappingExample.sqlite"];
[RKObjectManager setSha开发者_如何学PythonredManager:objectManager];
RKObjectMappingProvider* provider = [[RKObjectMappingProvider new] autorelease];
RKObjectMapping* verbMapping = [RKObjectMapping mappingForClass:[Verbs class]];
[verbMapping mapKeyPath:@"lemma" toAttribute:@"lemma"];
[verbMapping mapKeyPath:@"value" toAttribute:@"value"];
[provider setMapping:verbMapping forKeyPath:@"magic_verbs"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/api/users/1/magic_words" objectMapping:verbMapping delegate:self];
I have done the same for the adjs mapping. I also have a class called words that contains 2 nsarrays that is to contain the object mapping data. but im unsure how to implement this and link them up correctly.
@interface Words : NSObject {
NSArray *_verbs;
NSArray *_adjs;
}
@property (nonatomic, retain) NSArray *verbs,*adjs;
@end
any help and guidance on this is appreciated. I have looked at the example in catalog project and have been able to get that running but havent been able to master the concept to apply it to my own json files.
thanks
G
I suggest you to look at the new documentation of OM 2.0 that is in the Github page here if you haven't noticed yet. It clearly lays out on how to map your JSON to an object.
精彩评论