使用以下配置方法设置restKit
private func createCoreDataStack(){
var managedObjectModel = NSManagedObjectModel.mergedModelFromBundles(nil);
var managedObjectStore = RKManagedObjectStore(managedObjectModel: managedObjectModel)
var error: NSError?;
var success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (success == false) {
// RKLogError("Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
var path:String! = RKApplicationDataDirectory().stringByAppendingPathComponent("DB.sqlite")
var options = [NSMigratePersistentStoresAutomaticallyOption: NSNumber(bool: true), NSInferMappingModelAutomaticallyOption: NSNumber(bool: true)]
var persistentStore = managedObjectStore.addSQLitePersistentStoreAtPath(path, fromSeedDatabaseAtPath: nil, withConfiguration: nil, options: options, error: &error)
if (persistentStore == nil) {
// RKLogError("Failed adding persistent store at path '%@': %@", path, error);
}
managedObjectStore.createManagedObjectContexts()
managedObjectStore.managedObjectCache = RKInMemoryManagedObjectCache(managedObjectContext: managedObjectStore.persistentStoreManagedObjectContext)
var client = AFHTTPClient(baseURL: NSURL(string: "https://www.google.com/api"))
client.allowsInvalidSSLCertificate = true;
objectManager = RKObjectManager(HTTPClient: client)
objectManager.managedObjectStore = managedObjectStore
managedObjectStore.createPersistentStoreCoordinator()
RKObjectManager.setSharedManager(objectManager)
addMapping()
}
还使用addMapping()方法为我的主实体创建了一个间隙, 像这样:
private func addMapping(){
var mapping = RKEntityMapping(forEntityForName: "EntityName", inManagedObjectStore: objectManager.managedObjectStore)
mapping.identificationAttributes = ["id"]
mapping.addAttributeMappingsFromDictionary(["id":"id","uid":"uid","name":"name" ...])
//more code goes here
运行代码时,它会在addMapping()方法(addAttributeMappingFromDictionary)中提供的最后一行崩溃,但异常如下: *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* setObjectForKey:key不能为nil'
这是堆栈跟踪的一部分:
2 CoreFoundation 0x0000000105013af8 - [__ NSDictionaryM setObject:forKey:] + 968 3 FoodBooking 0x00000001020c2279 __50- [RKPropertyInspector propertyInspectionForClass:] _ block_invoke69 + 82 4 libdispatch.dylib 0x0000000106cf4614 _dispatch_client_callout + 8 5 libdispatch.dylib 0x0000000106cda002 _dispatch_barrier_sync_f_invoke + 365 6 AppName 0x00000001020c20b5 - [RKPropertyInspector propertyInspectionForClass:] + 886 7 AppName 0x00000001020c23d5 - [RKPropertyInspector classForPropertyNamed:ofClass:isPrimitive:] + 62 8 AppName 0x0000000102081e5b - [RKEntityMapping classForKeyPath:] + 309
我不知道发生了什么,并且对所有人和任何建议都是开放的