Restkit 0.20 NSDictionary返回而不是映射对象

时间:2013-01-26 11:08:16

标签: objective-c restkit

我正在使用restkit 0.20并且从GET返回的对象不属于Category类型但是NSDictionary?

这是我的模特

 

#import <Foundation/Foundation.h>

@interface Category : NSObject 

@property (nonatomic, copy) NSString* identifier;
@property (nonatomic, copy) NSString* description;
@property (nonatomic, copy) NSString* name;

@end

json

#import <Foundation/Foundation.h>

@interface Category : NSObject 

@property (nonatomic, copy) NSString* identifier;
@property (nonatomic, copy) NSString* description;
@property (nonatomic, copy) NSString* name;

@end

映射 [ { "category": { "_id": "50fc579d81d79d4db0000001", "name": "Category 0" } }, { "category": { "_id": "50fc579d81d79d4db0000006", "name": "Category 0" } } ]

RKObjectMapping *categoryMapping = [RKObjectMapping requestMapping];
[categoryMapping addAttributeMappingsFromDictionary:@{@"category._id": @"identifier", @"category.name": @"name"}];
[objectManager addRequestDescriptor: [RKRequestDescriptor requestDescriptorWithMapping:categoryMapping objectClass:[Category class] rootKeyPath:nil]];
RKResponseDescriptor *categoryResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:categoryMapping pathPattern:@"/categories" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:categoryResponseDescriptor];

如果我调用[RKObjectManager.sharedManager getObjectsAtPath:@“/ categories”,我得到一个返回的数组:)但是这些对象不属于Category类型,而是NSDictionary。


    NSLog(@"%@",[cat class]);
    2013-01-26 12:01:17.247 app[18943:c07] __NSDictionaryM

如果我调用dat.name,应用程序将崩溃

2 个答案:

答案 0 :(得分:0)

是的,返回的对象总是字典,您应该将它们映射到类:

Category* c= [Category new];
c.identifier= dict[@"_id"];
c.name= dict[@"name"];

每个字典都来自你得到的数组。

答案 1 :(得分:0)

哎呦



    RKObjectMapping* catMapping = [RKObjectMapping requestMapping];

我正在使用



    RKObjectMapping* catMapping = [RKObjectMapping mappingForClass:[Category class]];

我再次滚动:)