2016-09-24 91 views
1

斯威夫特版本3.斯威夫特副本 - 不工作

我對我的複製文件realm.default到開始時我的應用程序,如果不存在的話,這樣我可以打包爲分發的一部分工作。該文件需要可變,所以我將它複製到文檔目錄。

不幸的是我收到一個文件不存在的錯誤。我已經驗證路徑是否正確,並且.app文件中包含該文件。

就這麼說,文件有一個白色圓圈,上面有一行(不要輸入類型),並說我試圖打開它時無法在這種mac上打開它。我可以通過選擇顯示包內容來查看內容,並且文件包含在內。

上面的文字..

以下是從我的應用程序代理加載境界文件中的代碼:

func applicationWillTerminate(_ application: UIApplication) { 
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
} 

func openRealm() { 

    let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL! 
    let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm") 

    if !FileManager.default.fileExists(atPath: String(describing: defaultRealmPath)) { 
     do 
     { 
      try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: String(describing: defaultRealmPath)) 
     } 
     catch let error as NSError { 
     // Catch fires here, with an NSError being thrown 
     print("error occurred, here are the details:\n \(error)") 
     } 
    } 
} 

以下是錯誤消息(粗體顯示的情況下):

發生錯誤,這裏是詳細信息: 錯誤域= NSCocoaErrorDomain代碼= 4「文件」default.realm「不存在。」 UserInfo = {NSSourceFilePathErrorKey = /Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app /default.realm,NSUserStringVariant =( Copy ),NSDestinationFilePath = file:/// Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Data/Application /565716DD-E903-409F-B7C8-20154B0DF6BA/Documents/default.realm,NSFilePath =/Users/username/Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957/AppName.app/default.realm,NSUnderlyingError = 0x618000241bc0 {Error Domain = NSPOSIXErrorDomain Code = 2「No such file or directory」}}

任何人對此有任何想法。用戶/用戶名/ Library/Developer/CoreSimulator/Devices/4965D609-499A-4AE5-8ECC-3266DAE4BA87/data/Containers/Bundle/Application/B324E72E-2B5A-4A02-BC2C-FB542FDA6957 /AppName.app/存在,然後該文件包含在軟件包中。

+0

任何人有任何事情嗎?不幸的是我仍然卡住了。 – Apocal

回答

2

因爲你很困惑pathURLRealm.Configuration.defaultConfiguration.fileURL返回URL的實例。 Bundle.main.path()返回String的實例。 URL的字符串表示與path不同。

例如

print(String(describing: defaultRealmPath)) 
// => file:///Users/.../Documents/default.realm 

print(defaultRealmPath.path) 
// => /Users/.../Documents/default.realm 

所以你應該使用任何一個(路徑或URL)。如果你使用path,使用defaultRealmPath.path代替String(describing: defaultRealmPath)類似如下:

let defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL! 
let bundleReamPath = Bundle.main.path(forResource: "default", ofType:"realm") 

if !FileManager.default.fileExists(atPath: defaultRealmPath.path) { 
    do 
    { 
     try FileManager.default.copyItem(atPath: bundleReamPath!, toPath: defaultRealmPath.path) 
    } 
    catch let error as NSError { 
     // Catch fires here, with an NSError being thrown 
     print("error occurred, here are the details:\n \(error)") 
    } 
} 

如果使用URLBundle.main.url()代替Bundle main.path(): 讓defaultRealmPath = Realm.Configuration.defaultConfiguration.fileURL!

let bundleReamPath = Bundle.main.url(forResource: "default", withExtension:"realm")! 

if !FileManager.default.fileExists(atPath: defaultRealmPath.path) { 
    do 
    { 
     try FileManager.default.copyItem(at: bundleReamPath, to: defaultRealmPath) 
    } 
    catch let error as NSError { 
     // Catch fires here, with an NSError being thrown 
     print("error occurred, here are the details:\n \(error)") 
    } 
} 
+0

非常感謝Kishikawa – Apocal