2015-10-20 63 views
1

我最近升級到Swift 2.0,現在我遇到了領域問題。最近,我遇到了一個問題,應用程序,當它到達的第一次出現瞬間崩潰「!試試境界()」,導致這個錯誤:領域運行時錯誤

fatal error: 'try!' expression unexpectedly raised an error: Error Domain=io.realm Code=2 "open() failed: No such file or directory" UserInfo={NSFilePath=/Users/XXXXX/Library/Developer/CoreSimulator/Devices/7299DF18-E7D5-4499-93DD-A5035FB48E67/data/Containers/Data/Application/BED64819-5895-407F-9E90-9888741E24EB/Documents/default.realm, NSLocalizedDescription=open() failed: No such file or directory, Error Code=2}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.0.59/src/swift/stdlib/public/core/ErrorType.swift, line 50 (lldb)

我看到一個其他後有些與此有關,但它沒有幫助,因爲我不是直接調用路徑,而是拋出這個錯誤。

謝謝

回答

3

當您在使用雨燕try!,你選擇忽略,你可以以其他方式從恢復的錯誤。

在這種情況下,Realm初始值設定項標記爲throws。下面是從境界的文檔上Error Handling的摘錄:

Like any disk IO operation, creating a Realm instance could sometimes fail if resources are constrained. In practice, this can only happen the first time a Realm instance is created on a given thread. Subsequent accesses to a Realm from the same thread will reuse a cached instance and will always succeed.

To handle errors when first accessing a Realm on a given thread, use Swift’s built-in error handling mechanism:

do { 
    let realm = try Realm() 
} catch let error as NSError { 
    // handle error 
} 
-1

同樣的事情發生在我身上時,我手動刪除的對象從域通過瀏覽器領域。 這裏是我的兩分錢:刪除realm.lock和其他日誌文件並重新啓動應用程序爲我工作。看看屏幕截圖:

enter image description here