2012-08-22 223 views
1

我想映射和存储使用restkit对象映射的响应json数据。不幸的是我无法映射响应数据。当我认为我的数据库中的数据都是存储,但我得到一个崩溃与下面的消息.. 这个类不是键值编码兼容的关键Restkit的映射iphone

输出响应

{ 
    "users": [ 
     { 
      "id": "123456", 
      "ne": "JohnSmith", 
      "el": "[email protected]", 
      "dob": "1985/02/04", 
      "profile": { 
       "id": "654321", 
       "ht": "170cm", 
       "wt": "70kg" 
      } 
     } 
    ] 
} 

我使用这样的映射

RKManagedObjectMapping *userProfileMapping = [RKManagedObjectMapping mappingForClass:[GHSUser class] inManagedObjectStore:manager.objectStore]; 
    [userProfileMapping mapKeyPath:@"id" toAttribute:@"userId"]; 
    [userProfileMapping mapKeyPath:@"ne" toAttribute:@"name"];  
    [userProfileMapping mapKeyPath:@"el" toAttribute:@"email"]; 
    [userProfileMapping mapKeyPath:@"dob" toAttribute:@"dob"]; 

RKManagedObjectMapping *standardProfileMapping = [RKManagedObjectMapping mappingForClass:[GHSProfile class] inManagedObjectStore:manager.objectStore]; 

    [standardProfileMapping mapKeyPath:@"id" toAttribute:@"userId"]; 
    [standardProfileMapping mapKeyPath:@"ht" toAttribute:@"height"]; 
    [standardProfileMapping mapKeyPath:@"wt" toAttribute:@"weight"]; 

[manager.mappingProvider setMapping:standardProfileMapping forKeyPath:@"users.profile"]; 

//这里的keyPath(users.profile)我得到崩溃,但轮廓细节分贝插入。当我将keypath更改为配置文件时,我没有收到任何崩溃,但配置文件细节未插入到数据库中。

[userProfileMapping mapRelationship:@"users" withMapping:standardProfileMapping]; 

错误消息:

由于未捕获的异常 'NSUnknownKeyException'

*终止应用程序,原因是:“[< _NSObjectID_48_2 0x888a8d0>的setValue:forUndefinedKey:]:这个类不是关键值coding-符合关键配置文件。'

回答

1

这是因为你的users参数是一个NSArray,而不是一个NSDictionary。它不知道使用哪个users.profile

你想改变在同一行:

[manager.mappingProvider setMapping:standardProfileMapping forKeyPath:@"users.profile"]; 

到:

[manager.mappingProvider setMapping:userProfileMapping forKeyPath:@"users"]; 

这行添加到您的userProfileMapping:

[userProfileMapping mapKeyPath: @"profile" toRelationship: @"profile" withMapping: standardProfileMapping] 
+0

thanx的乌拉圭回合的反应。实际上我希望数组中的所有数据都存储在数据库中。我已经尝试了最后一个代码。但配置文件数据没有插入到数据库中。只有用户数据存储 – Anish

+0

我编辑我的答案,以适应您的要求 –

+0

我试过上面的代码,但个人资料数据没有插入 – Anish