I have some functions like this:
func getAllEntities() -> [MyEntity]? {
let fetchRequest = NSFetchRequest(entityName:"MyEntity")
var error: NSError?
let fetchedResults = context.executeFetchRequest(fetchRequest) as! [MyEntity]?
if let results = fetchedResults {
return results
}
else {
print("Could not fetch \(error), \(error!.userInfo)")
}
}
return nil
}
And now with the upgrade to Swift 2
and Xcode 7
, I get errors like these:
Cannot downcast from '[AnyObject]' to a more optional type '[MyEntity]?'
Call can throw, but it is not marked with 'try' and the error is not handled
This is after having performed the automatic Swift
code migration you are asked to do when you first start Xcode 7
. What is the correct way to re-write my function in the new Swift
version?
Thanks
EDIT: I need to keep backwards compatibility with iOS 7
and iOS 8
.