我只是无法弄清楚这个错误:
由于未捕获的异常“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
}
盯着这个看了好几个小时,看不到我缺少什么键