Igy
Igy

Reputation: 129

Core data error handling

Playing with core data and got stuck with this in Swift 2.0

var error: NSError?

    let fetchedResults =
    managedContext.executeFetchRequest(fetchRequest,
       error: &error) as? [NSManagedObject]

Xcode error says "Extra argument in call" and when I remove error:&error it says "error not handled", so what's the new syntax?

Upvotes: 1

Views: 630

Answers (1)

Code Different
Code Different

Reputation: 93141

In Swift 2.0 you have to use exception handling:

var fetchedResults : [NSManagedObject]?

do {
    fetchedResults = try managedContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]
} catch let error as NSError {
    print("Error \(error.localizedDescription)")
}

Apple documentation

Upvotes: 2

Related Questions