我正在关注 AppCoda 的一本好书,它在 IOS8 上运行,但现在出现很多错误:
// Load menu items from database
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
let fetchRequest = NSFetchRequest(entityName: "MenuItem")
var e: NSError?
menuItems = managedObjectContext.executeFetchRequest(fetchRequest, error: &e) as! [MenuItem]
if e != nil {
println("Failed to retrieve record: \(e!.localizedDescription)")
}
}
转换到 IOS9 后:
// Load menu items from database
if let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext {
let fetchRequest = NSFetchRequest(entityName: "MenuItem")
let e: NSError?
menuItems = (try! managedObjectContext.executeFetchRequest(fetchRequest)) as! [MenuItem]
if e != nil {
print("Failed to retrieve record: \(e!.localizedDescription)")
}
}
所以我改回“let by var”并且仍然收到警告 = Variable 'e' was never mutated;考虑更改为 let 常量。我在这个 block 中需要 var,我怎样才能摆脱这个警告? 任何帮助都非常受欢迎
最佳答案
新的 try
意味着 error
没有被使用,事实上你的代码并没有真正使用它,你只是定义它然后检查是否发生了什么有了它,这永远不会是真的。您应该删除 error
并使用 catch
来处理问题情况。
关于ios - CoreData IOS9 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33071436/