ResKit对象映射嵌套对象

时间:2015-04-19 23:13:56

标签: ios objective-c xcode restkit

我正在尝试使用RestKit来解析一些JSON。该应用程序用于跟踪比赛,并具有场地,游戏和玩家的概念。游戏有赢家和输家(都是球员)和场地。 Web服务返回的JSON如下所示:

[{"id":1,"winner":{"name":"NAME2","id":2,"picture_url":"CK.png"},"loser":{"name":"NAME3","id":3,"picture_url":"NJ.png"},"venue":{"name":"Venue 1","id":2}},
{"id":2,"winner":{"name":"NAME2","id":2,"picture_url":"CK.png"},"loser":{"name":"NAME1","id":1,"picture_url":"NC.png"},"venue":{"name":"Venue 1","id":2}},
{"id":3,"winner":{"name":"NAME2","id":2,"picture_url":"CK.png"},"loser":{"name":"NAME3","id":3,"picture_url":"NJ.png"},"venue":{"name":"Venue 1","id":2}},
{"id":4,"winner":{"name":"NAME2","id":2,"picture_url":"CK.png"},"loser":{"name":"NAME1","id":1,"picture_url":"NC.png"},"venue":{"name":"Venue 1","id":2}}]

我的对象头文件如下所示:

# PTGame.h
@interface PTGame : NSObject
@property (nonatomic, assign) NSNumber *uid;
@property (nonatomic, assign) PTPlayer *winner;
@property (nonatomic, assign) PTPlayer *loser;
@property (nonatomic, assign) PTVenue *venue;
@end

# PTVenue.h
@interface PTVenue : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSNumber *uid;
-(CLLocation *)location;
-(CLLocationDistance)distanceFromLocation:(CLLocation *)location;
@end

# PTPlayer.h
@interface PTPlayer : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *pictureUrl;
@property (nonatomic, assign) NSNumber *uid;
@end

最后,我的映射设置如下:

RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[PTVenue class]];
[venueMapping addAttributeMappingsFromDictionary:@{@"name"      : @"name",
                                                   @"id"        : @"uid"}];

RKObjectMapping *playerMapping = [RKObjectMapping mappingForClass:[PTPlayer class]];
[playerMapping addAttributeMappingsFromDictionary:@{@"name"       : @"name",
                                                    @"id"         : @"uid",
                                                    @"picture_url": @"pictureUrl"}];

// Add Object Mapping for PTGame
RKObjectMapping *gameResponseMapping = [RKObjectMapping mappingForClass:[PTGame class]];

// Setup Game Object relationships
[gameResponseMapping addAttributeMappingsFromDictionary:@{@"id": @"uid"}];
[gameResponseMapping addRelationshipMappingWithSourceKeyPath:@"winner" mapping:playerMapping];
[gameResponseMapping addRelationshipMappingWithSourceKeyPath:@"loser" mapping:playerMapping];
[gameResponseMapping addRelationshipMappingWithSourceKeyPath:@"venue" mapping:venueMapping];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

RKResponseDescriptor *gameListResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:gameResponseMapping
                                                                                                method:RKRequestMethodGET
                                                                                           pathPattern:@"/games.json"
                                                                                               keyPath:nil
                                                                                           statusCodes:statusCodes];
[objectManager addResponseDescriptor:gameListResponseDescriptor];

这一切对我来说都是正确的。返回结果时不会抛出任何错误,但稍后在尝试访问游戏属性时会出现异常。通过XCode调试器查看对象显示:XCode Objects

似乎有些东西被打破了 - 为什么这些字符串显示为“赢家”和“场地”。为什么uid为空?每次发出请求时,对象看起来都不一样,这让我相信内存处理不当。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

Wain的评论让我走上了寻找映射代码之外的道路。我查看了RestKit Object Mapping docs,发现所有属性都使用nonatomiccopy属性,而不是assign。此外,对象引用(例如PTPlayer *winner只是nonatomic。更改我的代码以反映这一问题解决了这个问题。

生成的头文件如下所示:

# PTPlayer.h
@interface PTPlayer : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *pictureUrl;
@property (nonatomic, copy) NSNumber *uid;
@end

# PTVenue.h
@interface PTVenue : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSNumber *uid;
@property (nonatomic, assign) CGFloat longitude;
@property (nonatomic, assign) CGFloat latitude;
-(CLLocation *)location;
-(CLLocationDistance)distanceFromLocation:(CLLocation *)location;
@end


# PTGame.h
@interface PTGame : NSObject
@property (nonatomic, copy) NSNumber *uid;
@property (nonatomic) PTPlayer *winner;
@property (nonatomic) PTPlayer *loser;
@property (nonatomic) PTVenue *venue;
@end