Alek Piasecki Alek Piasecki - 2 months ago 28
Swift Question

Error.code no longer valid syntax in swift 3?

Recently converted my app from the previous version to swift 3. Predictably, this caused a lot of errors in my code. I do not know how to fix the following one:
(In the

Appdelegate.swift
):

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
if error.code == 3010 {
print("Push notifications are not supported in the iOS Simulator.\n")
} else {
print("application:didFailToRegisterForRemoteNotificationsWithError: %@\n", error)
}
}


The error given is that the value of type error has no member "code". How do I get around this?

Sam Sam
Answer

Try casting error to NSError

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    if (error as NSError).code == 3010 {
        print("Push notifications are not supported in the iOS Simulator.\n")
    } else {
        print("application:didFailToRegisterForRemoteNotificationsWithError: %@\n", error)
    }
}