ZAFAR007 ZAFAR007 - 5 months ago 18
iOS Question

Nil is not compatible with expected argument type '()'

class func saveManagedObjectContext(managedObjectContext:NSManagedObjectContext)->Bool{
if managedObjectContext.save(nil){
return true
}else{
return false
}
}



Nil is not compatible with expected argument type '()'

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


enter image description here

How i can fix it in swift2?

Thanks

Answer

Remove nil from the argument list. The method managedObjectContext.save() throws an error in case something goes wrong. The right way of doing it is

do{
    try managedObjectContext.save()
    return true
}
catch{
    return false
}