RestKit - 不符合关键值编码

时间:2013-05-21 14:13:42

标签: objective-c core-data restkit-0.20

我有一个托管对象MyEntity,我在整个应用程序中使用它没有任何问题。

@interface MyEntity : NSManagedObject

@property (nonatomic, retain) NSString *code;
@property (nonatomic, retain) NSNumber *def_value;
@property (nonatomic, retain) NSString *label;
@property (nonatomic, retain) NSString *link_label;
@property (nonatomic, retain) NSNumber *order;

@end

我有一个网络服务http://my_server/MyEntity,它返回我:

[
    {
        CODE: "TYPE",
        LABEL: "Administrative",
        ORDER: "3",
        DEF_VALUE: "0",
        LINK_LABEL: ""
    },
    {
        CODE: "TOPIC",
        LABEL: "NDF",
        ORDER: "1",
        DEF_VALUE: "0",
        LINK_LABEL: "Administrative"
    },
    {
        CODE: "TOPIC",
        LABEL: "Report",
        ORDER: "2",
        DEF_VALUE: "1",
        LINK_LABEL: "Administrative"
    },
    {
        CODE: "TOPIC",
        LABEL: "Other",
        ORDER: "3",
        DEF_VALUE: "0",
        LINK_LABEL: "Administrative"
    }, ...
]

我正在尝试映射并获取此列表

NSURL *baseUrl = [NSURL URLWithString: @"http://my_server"];
NSString *objName = @"MyEntity";
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
[RKManagedObjectStore setDefaultStore:managedObjectStore];
[managedObjectStore createPersistentStoreCoordinator];

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL: baseUrl];
RKEntityMapping *objMap = [RKEntityMapping mappingForEntityForName:objName inManagedObjectStore:managedObjectStore];
[objMap addAttributeMappingsFromDictionary: @{
     @"CODE": @"code",
     @"LABEL": @"label",
     @"ORDER": @"order",
     @"DEF_VALUE": @"def_value",
     @"LINK_LABEL": @"link_label",
 }];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objMap pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:responseDescriptor];
[objectManager getObjectsAtPath:objName parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"ok");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"ko");
}];

它给了我这个错误

  

我restkit.network:RKHTTPRequestOperation.m:185 GET 'http://my_server/MyEntity'(200 OK)   CoreData:错误:无法在NSManagedObject类'MyEntity'上调用指定的初始化程序   因未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:
  '[valueForUndefinedKey:]:实体(null)不是密钥“link_label”的密码值编码。'

我在2或3个SO问题上看到了这个错误,但每个问题都有不同的问题,看起来不是我的问题。

有人可以帮我调试吗?

1 个答案:

答案 0 :(得分:2)

您需要完全配置objectManager

objectManager.managedObjectStore = managedObjectStore;

您还没有完成核心数据堆栈初始化:

[managedObjectStore createManagedObjectContexts];