10

I recently updated Xcode to the new 7.0 beta.

I did the migration with the assistant but there are a few more issues.

func saveContext () {
    if let moc = self.managedObjectContext {
        var error: NSError? = nil
        if moc.hasChanges && !moc.save() {
            NSLog("Unresolved error \(error), \(error!.userInfo)")
            abort()
        }
    }
}

On line 4 there are 4 issues: the first one is:

Binary operator '&&' cannot be applied to two Bool operands

the second one is:

Call can throw, but it is not marked with 'try' and the error is not handled

Can someone please help me?

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
Luke Pistrol
  • 1,199
  • 3
  • 12
  • 26
  • 1
    This question has a much better title than does the duplicate, as programmers are likely to google on this confusing error message. For reference, the following declaration gives the same error message: func test() { return true && true } , Notice that the function is declared as void , i.e. ->Bool is missing. – ragnarius Nov 21 '15 at 09:40

1 Answers1

2

Here is some code that should do the trick. Remember to preceed throw statements with try and catch them.

func saveContext () {
    if let moc = self.managedObjectContext {
        if moc.hasChanges  {
            do {
                try moc.save()
            } catch {
                NSLog("Unresolved error \(error)")
                abort()
            }
        }
    }
}
John Difool
  • 5,572
  • 5
  • 45
  • 80