Reputation: 479
I am following a great book from AppCoda, it was working on IOS8 but give many errors now:
// 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)")
}
}
After Conversion to 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)")
}
}
So I change back "let by var" and still get a warning = Variable 'e' was never mutated; consider changing to let constant. I need var in this block, how can I get rid of this warning? Any help is more than welcome
Upvotes: 0
Views: 149
Reputation: 80271
This is how it is done now:
...
do {
let items = try context.executeFetchRequest(request)
// do something with items
}
catch { /* react to error */ }
Upvotes: 0
Reputation: 119041
The new try
means that the error
isn't used, and indeed your code isn't using it really, you just define it and then check if anything happened with it, which will never be true. You should delete the error
and use catch
to handle problem situations.
Upvotes: 2