0

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

1
  • Why do you need it to be a var if you're never mutating it? Commented Oct 12, 2015 at 4:14

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

This is how it is done now:

... do { let items = try context.executeFetchRequest(request) // do something with items } catch { /* react to error */ } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.