Swift - 将支持iCloud的持久存储添加到核心数据

时间:2014-12-18 12:06:59

标签: core-data swift store icloud persistent

var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
let documentsDirectory:NSURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL
let storeUrl:NSURL = documentsDirectory.URLByAppendingPathComponent("CoreData.sqlite")
var error:NSError?
let coordinator:NSPersistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: appDel.managedObjectModel)
let storeOptions:NSDictionary = [NSPersistentStoreUbiquitousContentNameKey:"MyAppCloudStore"]
let store:NSPersistentStore = coordinator.addPersistentStoreWithType("NSSQLiteStoreType", configuration: nil, URL: storeUrl, options: storeOptions, error: &error)!
let finaliCloudURL = store.URL

这给我带来了一个错误:

2014-12-18 13:55:59.160 TestApp[1410:314557] CoreData: error: -addPersistentStoreWithType:NSSQLiteStoreType configuration:(null) URL:file:///Users/Bogdan-iMac/Library/Developer/CoreSimulator/Devices/29BD765A-1F4A-4C8A-8EAE-4AA0B1E91918/data/Containers/Data/Application/F19B3C5D-B6AD-4944-B4F3-BBBE4AF36D66/Documents/CoreData.sqlite options:{
    NSPersistentStoreUbiquitousContentNameKey = MyAppCloudStore;
} ... returned error Error Domain=NSCocoaErrorDomain Code=134080 "The NSSQLiteStoreType persistent store type does not support iCloud integration." UserInfo=0x79fc9360 {NSLocalizedDescription=The NSSQLiteStoreType persistent store type does not support iCloud integration.} with userInfo dictionary {
    NSLocalizedDescription = "The NSSQLiteStoreType persistent store type does not support iCloud integration.";
}
fatal error: unexpectedly found nil while unwrapping an Optional value

我在服务功能中启用了iCloud:键值存储

1 个答案:

答案 0 :(得分:2)

NSSQLiteStoreType是已定义的常量值。您需要使用NSSQLiteStoreType,而不是@NSSQLiteStoreType"。虽然NSSQLiteStoreType是字符串,但其值不是@"NSSQLiteStoreType"