我正在尝试加载与我的用户对象具有多对一关系的实体列表。在这种情况下,User对象包含需要映射的通道列表(首先仅实例化通道ID,然后再延迟加载)API返回数组中通道的ID然后我尝试转换该数组使用RKRelationshipMapping进入通道数组。但是,它不断抛出
[< __ NSCFString 0x7ff0f243ece0> valueForUndefinedKey:]:此类不是密钥值的密钥值
这是JSON
{"id":"10","createDate":1412222248943,"modDate":1412222269588,"emailAddress":"chentsen1005@gmail.com","firstName":"test_account","lastName":"test_account_lastName","password":"****","referralCode":"password","channelSubscriptions":["1","2","3"]}
这是我的映射代码(在swift中,但不应该重要)
class func mapReadUsersEndpoint(managedObjectStore : RKManagedObjectStore){
var statusCodes = RKStatusCodeIndexSetForClass(UInt(RKStatusCodeClassSuccessful))
var userGetRequestMapping = RKObjectMapping.requestMapping()
userGetRequestMapping.addAttributeMappingsFromDictionary(["id":"id"])
var userGetRequestDescriptor = RKRequestDescriptor(mapping: userGetRequestMapping,
objectClass: User.self,
rootKeyPath: nil,
method : RKRequestMethod.GET)
var userGetResponseMapping = RKEntityMapping(forEntityForName: "User", inManagedObjectStore: managedObjectStore)
userGetResponseMapping.addAttributeMappingsFromDictionary(["firstName":"firstName",
"lastName":"lastName",
"emailAddress":"email",
"referralCode":"referralCode",
"password":"password"])
var channelMapping = RKEntityMapping(forEntityForName: "Channel", inManagedObjectStore: managedObjectStore)
channelMapping.addAttributeMappingsFromDictionary(["" : "channel_id"])
var channelRelationshipMapping = RKRelationshipMapping(fromKeyPath: "channelSubscriptions", toKeyPath: "subscribedChannels", withMapping: channelMapping)
userGetResponseMapping.addPropertyMapping(channelRelationshipMapping)
var userGetResponseDescriptor = RKResponseDescriptor(mapping: userGetResponseMapping, method: RKRequestMethod.GET, pathPattern: "/resources/account/:id/", keyPath: nil, statusCodes: statusCodes)
RKObjectManager.sharedManager().addRequestDescriptor(userGetRequestDescriptor)
RKObjectManager.sharedManager().addResponseDescriptor(userGetResponseDescriptor)
,最后是错误信息
Performing mapping operation: <RKMappingOperation 0x7ff0f278ad00> for 'Channel' object. Mapping values from object 1 ({
HTTP = {
request = {
URL = "http://localhost:8080/resources/account/10/";
headers = {
Accept = "application/json";
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
"User-Agent" = "Hip/1 (iPhone Simulator; iOS 8.0; Scale/2.00)";
};
method = GET;
};
response = {
URL = "http://localhost:8080/resources/account/10/";
headers = {
"Content-Type" = "application/json";
Date = "Thu, 02 Oct 2014 04:22:41 GMT";
Server = "Jetty(9.2.2.v20140723)";
"Transfer-Encoding" = Identity;
};
};
};
mapping = {
collectionIndex = 0;
rootKeyPath = "<null>";
};
})
to object <Channel: 0x7ff0f279f340> (entity: Channel; id: 0x7ff0f279f3b0 <x-coredata:///Channel/tBBBCA84F-8C28-4729-8E06-A93802EFE14E2> ; data: {
backgroundColor = 0;
backgroundImage = nil;
channelDescription = nil;
"channel_id" = nil;
followers = 0;
imageData = nil;
isPublic = nil;
name = nil;
referralSource = nil;
subscribers = (
);
threads = (
);
}) with object mapping (null)
2014-10-02 00:22:41.789 Hip[99685:3770746] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x7ff0f243ece0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key .'
我在这里很迷失 - 看起来我已经关注了Github页面上的所有内容。任何建议都非常感谢。感谢