1

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?

Runeaway3
  • 1,439
  • 1
  • 17
  • 43

2 Answers2

4

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)
    }
}
Sam
  • 159
  • 1
  • 7
0

in swift2+, the function named

optional func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError)

in swift3+, the function named

optional func application(_ application: NSApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)

the type of error change from NSError to Error.

the code is the property of NSError