Restkit, Swift, Core data 一对多
Posted
技术标签:
【中文标题】Restkit, Swift, Core data 一对多【英文标题】:Restkit, Swift, Core data one to many 【发布时间】:2015-08-31 12:28:42 【问题描述】:我就是想不通这个错误:
由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'*** setObjectForKey: key cannot be nil
这里是一些示例 JSON
["storeId":"22","storeName":"Jewel Osco","storeAddress1":"890 N Western Ave","storeAddress2":"","storeCity":"Lake Forest","storeState":"IL","storeZipCode":"","storeGfCount":"71","storeListCount":"2","storeDistance":"1.13","products":["storeId":"22","productId":"61","upcCode":"00090800000751","brandDesc":"Herb Pharm","productName":"Herb Pharm Black Elderberry Herbal Glycerite Extract","storeId":"22","productId":"64","upcCode":"00698997807308","brandDesc":"Udi's","productName":"Udi's Soft N' Chewy Granola Bars Chocolate Chip - 5 CT"]]
这是一个包含一对多产品数组的商店数组。
如果我在没有关系的情况下运行查询,它将完美运行。但是,当我添加关系时,它会失败。
这是相关的映射:
class func defaultMapping(objectManager:RKObjectManager!) -> RKEntityMapping
let managedObjectStore:RKManagedObjectStore = objectManager.managedObjectStore!
let defaultMapping = RKEntityMapping(forEntityForName: "LocatorStoreProduct", inManagedObjectStore: managedObjectStore)
defaultMapping.identificationAttributes = ["productId","storeId"]
let mappingDictionary:[String:String] = [
"productId":"productId" ,
"storeId":"storeId" ,
"upcCode":"upcCode" ,
"brandDesc":"brand" ,
"productName":"productName"
]
defaultMapping.addAttributeMappingsFromDictionary(mappingDictionary)
return defaultMapping
这里是映射的赋值:
class func defaultMapping(objectManager:RKObjectManager!) -> RKEntityMapping
let managedObjectStore:RKManagedObjectStore = objectManager.managedObjectStore!
let defaultMapping = RKEntityMapping(forEntityForName: "LocatorStore", inManagedObjectStore: managedObjectStore)
defaultMapping.identificationAttributes = ["extId"]
let mappingDictionary:[String:String] = [
"storeId":"extId" ,
"storeName":"storeName" ,
"storeAddress1":"address1" ,
"storeAddress2":"address2" ,
"storeCity":"city" ,
"storeState":"state" ,
"storeZipCode":"zipCode" ,
"stoerGfCount":"totalCount",
"storeListCount":"inListCount",
"storeDistance":"distance"
]
defaultMapping.addAttributeMappingsFromDictionary(mappingDictionary)
//HERE IS MY RELATIONSHIP MAPPING:
let lspMapping = LocatorStoreProduct.defaultMapping(objectManager)
let relationshipMapping = RKRelationshipMapping(fromKeyPath: "products", toKeyPath: "locatorStoreProducts", withMapping: lspMapping)
defaultMapping.addPropertyMapping(relationshipMapping)
return defaultMapping
盯着这个看了好几个小时,看不到我错过了什么键
【问题讨论】:
【参考方案1】:代码是正确的。我已将我的 obj 从 LocatorStoreProducts 重命名为 LocatorStoreProduct。在重构中遗漏了一个明显的地方
【讨论】:
以上是关于Restkit, Swift, Core data 一对多的主要内容,如果未能解决你的问题,请参考以下文章
RESTkit,Core Data:在将对象传输到 Core Data 之前对其进行处理