2015-06-29 5 views
8

Hallo, ich bin mein Projekt von Swift Aktualisierung mit Xcode Swift2 7 und ich erhalte diesen Fehler Coredata:Swift 2 Extra-Argument 'Fehler' in Call

Extra argument 'error' in call 

in dieser Linie

if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil { 

EDIT

dies ist mein Code:

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = { 
     // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail. 
     // Create the coordinator and store 
     var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) 
     let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Xipe_Tech.sqlite") 
     var error: NSError? = nil 
     var failureReason = "There was an error creating or loading the application's saved data." 

     do { 
      try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) 
      coordinator = nil 
      // 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 
      error = 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. 
      NSLog("Unresolved error \(error), \(error!.userInfo)") 
      abort() 

     } catch let error as NSError { 
      print(error.localizedDescription) 

     } 

     return coordinator 
    }() 

jetzt bekomme ich Fehler in der ersten Zeile Wie kann ich das beheben ?? Danke

+1

möglich Duplikat [Swift: Extra Argument 'Fehler' in Call] (http://stackoverflow.com/questions/31073497/swift-extra-argument-error-in-call) – Moritz

Antwort

13

Swift 2 bietet jetzt einen try/catch-Mechanismus, und Cocoa-APIs wurden neu geschrieben, um dies zu verwenden, anstatt den Fehler auf die traditionelle Objective-C-Art zurückzuführen.

Sie sollten dies jetzt tun:

do { 
    try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) 
} catch let error as NSError { 
    print(error.localizedDescription) 

} 
+0

jetzt i Fehler angezeigt in dieser Zeile: 'lazy var persistantStoreCoordinator: NSPersistentStoreCoordinator? = {'Der Fehler ist:' Call kann werfen, ist aber nicht als Versuch markiert und der Fehler wird nicht behandelt. – markutus

+0

Bitte zeigen Sie Ihren Code. Aber es scheint, als ob Sie nicht die Try-Annotation vor dem Aufruf der Funktion hinzufügen – agy

+0

ich meinen Code hinzufügen, danke – markutus

5

jemand anderes hat dieses Problem das nur unten. Ich hatte das gleiche Problem. Apple ersetzte das ursprüngliche "if coordinator! .addPersistentStoreWithType" durch einen do probieren Sie Schlagwort.

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. 
     NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") 
     abort() 
    } 

    return coordinator 
    }()