2015-10-20 115 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