Error.code在swift 3中不再有效语法?

时间:2016-09-23 16:31:35

标签: ios swift

最近将我的应用程序从之前的版本转换为swift 3.可以预见,这会在我的代码中导致很多错误。我不知道如何解决以下问题: (在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)
        }
    }

给出的错误是类型错误的值没有成员“代码”。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

尝试将错误转换为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)
    }
}

答案 1 :(得分:0)

在swift2 +中,名为

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

在swift3 +中,名为

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

error的类型从NSError更改为Error

codeNSError

的属性