我正在尝试映射来自具有 2 个属性的服务器的响应:
- 一个数字,它是数组中对象的计数
- 我已经在其他类中映射的数组用户对象。
这就是我拥有类(class)模型的方式:
及其与具有“参与者”关系的用户类相关。
我知道我要映射的嵌套类正在工作,因为我在其他类中使用它并且效果很好,但是这个实现不同,所以我不知道如何映射它。
我的课上有这个:
.h 类:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "NSManagedObject+Convenience.h"
@interface DUParticipant : NSManagedObject
@property (nonatomic, retain) NSNumber * participantsCount;
@property (nonatomic, retain) NSSet *participants;
+ (RKEntityMapping *)mapping;
+ (void)configMapping;
@end
.m 类
#import "DUParticipant.h"
#import "DUUser+Ext.h"
@implementation DUParticipant
@dynamic participantsCount;
@dynamic participants;
+ (RKEntityMapping *)mapping
{
RKEntityMapping* map = [super mapping];
[map addAttributeMappingsFromDictionary{
@"participants_count" : @"participantsCount"
}];
return map;
}
+(void) configMapping{
RKObjectManager* man= [RKObjectManager sharedManager];
RKEntityMapping* map = [self mapping];
[man addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:map pathPattern"/v1/users/participants" keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary{
@"participants_count" : @"participantsCount"
}];
[map addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath"participants"
toKeyPath"participants"
withMapping:[DUUser mapping]]];
[man addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[DUParticipant class] rootKeyPath"DUParticipant"]];
}
@end
而json的响应是:
{
"participants_count": 1,
"participants": [
{
"participant": {
"id": 3,
"gender": "M",
"name": "Oscar",
"lastname": "Lares",
"username": "CaLe",
"email": "[email protected]",
"bio": "UX Designer! Mac Geek, Computers, Gadgets and Movies lover!",
"created_at": "2012-07-28T18:34:27Z",
"updated_at": "2013-07-29T13:05:21Z",
"cover": null,
"avatar": {
"id": 108,
"original": "https://duuin.s3.amazonaws.com/uploads/image/file/108/photo.jpg",
"thumbnail": "https://duuin.s3.amazonaws.com/uploads/image/file/108/thumb_photo.jpg"
}
}
}
]
**** ** 编辑** ****
这是我在日志控制台中的内容,它是 json 响应:
response.body =
{
"participants_count": 2,
"participants": [
{
"participant": {
"id": 3,
"gender": "M",
"name": "blabla",
"lastname": "blabla",
"username": "blabla",
"email": "blabla",
"bio": "blablablablablabla",
"created_at": "2012-07-28T18:34:27Z",
"updated_at": "2013-07-29T13:05:21Z",
"cover": null,
"avatar": {
"id": 108,
"original": "blabla",
"thumbnail": "blabla"
}
}
},
{
"participant": {
"id": 1,
"gender": "M",
"name": "blabla",
"lastname": "blabla",
"username": "blabla",
"email": "blabla",
"bio": "blabla",
"created_at": "2012-07-22T20:30:18Z",
"updated_at": "2013-07-31T03:52:26Z",
"cover": {
"id": 21,
"original": "blabla",
"thumbnail": "blabla"
},
"avatar": {
"id": 19,
"original": "blabla",
"thumbnail": "blabla"
}
}
}
]
}
以及日志中的对象:
2013-08-01 13:35:43.949 duuin[24550:c07] Participant: <NSManagedObject: 0x16d7ef00> (entity: DUParticipant; id: 0x16d74170 <x-coredata://1C844BC7-AF17-4D27-90F9-9F5B8ACDF848/DUParticipant/p86> ; data: {
participants = nil;
participantsCount = 2;
})
Best Answer-推荐答案 strong>
我认为问题在于您的嵌套类映射。它可能在其他时候也能正常工作,但我猜上下文是不同的。在您显示的代码中,参与者位于字典中的 participant 键中。这需要反射(reflect)在映射中的所有键中。
大概你的映射当前有:
@"gender" -> @"gender"
但是对于您显示的 JSON,它必须是:
@"participant.gender" -> @"gender"
关于iphone - 对象映射 Restkit 不映射嵌套对象,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/17984176/
|