'ErrorType' no es convertible a 'NSError'

Tengo un error con Google Analytics:

'ErrorType' no es convertible a 'NSError'; ¿querías usar 'como'? para forzar abatido

Sucede cuando bash llamar 2 veces createScreenView

Hago esto:

override func viewDidLoad() { let tracker = GAI.shanetworkingInstance().defaultTracker tracker.set(kGAIScreenName, value: "Demande Gratuite") var builder = GAIDictionaryBuilder.createScreenView().build() as! [NSObject : AnyObject] tracker.send(builder) ... } @IBAction func Valider(sender: AnyObject) { ... let trackerv = GAI.shanetworkingInstance().defaultTracker trackerv.set(kGAIScreenName, value: "Demande Envoyé") var builder = GAIDictionaryBuilder.createScreenView().build() as! [NSObject : AnyObject] trackerv.send(builder) let eventTracker: NSObject = GAIDictionaryBuilder.createItemWithTransactionId( "1", name: "test", sku: nil, category: "IOS", price: 1, quantity: 1, currencyCode: nil).build() trackerv.send(eventTracker as! [NSObject : AnyObject]) } 

Función donde el error es:

 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { // The persistent store coordinator for the application. This implementation creates and returns 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 let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite") 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) } 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 }() 

Otro problema aquí:

 let tracker = GAI.shanetworkingInstance().defaultTracker tracker.set(kGAIScreenName, value: "Mentions Légales") var builder = GAIDictionaryBuilder.createScreenView().build() as! [NSObject : AnyObject] tracker.send(builder) 

Reparto forzado de 'NSMutableDictionary!' a '[NSObject: AnyObject]' siempre tiene éxito; ¿quiso usar 'como'?

Y

El 'generador' variable nunca fue mutado; considere cambiar a 'let' constante

Para mí, esto también ocurre cuando se utilizan AVFoundation y Core Data en el mismo proyecto.

Para deshacerse del error:

'ErrorType' no es convertible a 'NSError'; ¿querías usar 'como'? para forzar abatido

O las advertencias:

El reparto condicional de 'ErrorType' a 'NSError' siempre tiene éxito.

El reparto forzado de 'ErrorType' a 'NSError' siempre tiene éxito; ¿quiso usar 'como'?

Hice esto:

 lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { // The persistent store coordinator for the application. This implementation creates and returns 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 let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MY_APP_NAME.sqlite") 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) } catch let error as NSError { // 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 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() } catch { // dummy } return coordinator }() 

Espero que esto ayude 🙂

El post de error le indica el problema y sugiere una solución. El error constante en el bloque catch es del tipo ErrorType , y desea convertirlo en NSError , un yeso que puede no tener éxito. Por lo tanto, no puede usar el operador regular as solo para los models que el comstackdor puede contar siempre tendrá éxito. ¡En su lugar, debe utilizar as! para forzar-cast o as? para hacer una caja fuerte.

 catch { // Report any error we got. var dict = [String: AnyObject]() dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" dict[NSLocalizedFailureReasonErrorKey] = failureReason if let underlyingError = error as? NSError { dict[NSUnderlyingErrorKey] = underlyingError } 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() } 

Para su segundo problema, tiene el problema opuesto. ¡Estás usando el as! operador para un reparto que el comstackdor sabe que siempre funcionará. Deberías usar la llanura as operador. Y el tercer problema es que está declarando una variable ( var ) cuyo valor nunca cambia. En esos casos, se prefiere usar una constante ( let ).

 let builder = GAIDictionaryBuilder.createScreenView().build() as [NSObject : AnyObject]