Reputation: 4910
The AppDelegate xcode generates when I create a new project has errors within it which I'm struggling to debug as I'm new to swift. Why are generated files returning errors and how can I prevent this when creating new swift projects?
Error 1
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Swift_Conversion.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
coordinator = nil
//ERROR: EXTRA ARGUMENT 'ERROR' IN CALL
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error
error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
return coordinator
}()
Error 2
func saveContext () {
if let moc = self.managedObjectContext {
var error: NSError? = nil
if moc.hasChanges && !moc.save(&error) {
//ERROR: CANNOT CONVERT VALUE OF TYPE 'INOUT NSERROR?'(AKA 'INOUT OPTIONAL<NSERROR>') TO EXPECTED ARGUMENT TYPE '()'
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
}
}
I've attempted to manually debug this but my inexperience in both editing the AppDelegate and using swift have let me down as every error I remove is accompanied by some new errors.
Upvotes: 1
Views: 540
Reputation: 27620
The method signature of addPersistentStoreWithType
has changed. It is now throwable
and has to be used like this:
do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil)
} catch {
// Report any error we got.
var dict = [String: AnyObject]()
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
dict[NSLocalizedFailureReasonErrorKey] = failureReason
dict[NSUnderlyingErrorKey] = error as NSError
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
print("Unresolved error \(wrappedError), \(wrappedError.userInfo)")
abort()
}
Same with the save
method on NSManagedObjectContext
:
func saveContext () {
if managedObjectContext!.hasChanges {
do {
try managedObjectContext!.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
print("Unresolved error \(nserror), \(nserror.userInfo)")
abort()
}
}
}
Upvotes: 4