我正在尝试从服务器映射具有2个属性的响应:
这就是我拥有班级模型的方式:
并且它与具有“参与者”关系的用户类相关。
我知道我要映射的嵌套类正在工作,因为我在其他类中使用它并且工作得很好,但是这个实现是不同的所以我不知道如何映射它。
我的课程中有这个:
.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": "cale@duuin.com",
"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;
})
答案 0 :(得分:1)
我认为问题在于您的嵌套类映射。它可能在其他时间工作,但我想上下文是不同的。在显示的代码中,参与者位于字典内的participant
键内。这需要反映在映射中的所有键中。
据推测,您的映射目前有:
@"gender" -> @"gender"
但对于你所展示的JSON,它必须是:
@"participant.gender" -> @"gender"