2015-08-27 50 views
1

我嘗試按照「綁定領域與應用程序」文檔。我嘗試使用方法Realm().writeCopyToPath(_:encryptionKey:)),但問題是我無法在我的OSX上的特定位置上創建文件。它顯示錯誤,該文件已經退出。請給我用writeCopyToPathwriteCopyToPath Realm不起作用

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 
var window: UIWindow? 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { 
    window = UIWindow(frame: UIScreen.mainScreen().bounds) 
    window?.rootViewController = UIViewController() 
    window?.makeKeyAndVisible() 

    NSFileManager.defaultManager().removeItemAtPath(Realm.Configuration.defaultConfiguration.path!, error: nil) 

    // Create a standalone object 
    var mydog = Dog() 

    // Set & read properties 
    mydog.name = "Rex" 
    mydog.age = 9 
    println("Name of dog: \(mydog.name)") 

    // Realms are used to group data together 
    let realm = Realm() // Create realm pointing to default file 
    println(realm.writeCopyToPath("/Users/taforyou/CU-TEP",encryptionKey: nil)) 

    // Save your object 
    realm.beginWrite() 
    realm.add(mydog) 
    realm.commitWrite() 
    return true 
} 
} 

輸出日誌以正確的方式在運行

Name of dog: Rex 
Optional(Error Domain=io.realm Code=4 "open() failed: File exists" UserInfo=0x7fbc704a8cd0 {NSLocalizedDescription=open() failed: File exists, Error Code=4}) 

謝謝

境界0.95

回答

2

得到打印的錯誤消息說,這一切:文件您正嘗試創建(/Users/taforyou/CU-TEP)已存在。請注意,writeCopyToPath預期的路徑是生成的領域文件(/path/to/file.realm,而不是/path/to)的完整路徑,以防萬一CU-TEP實際上是一個目錄。

+0

哦,這可能會奏效,謝謝jpsim –