最近将我的应用程序从之前的版本转换为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)
}
}
给出的错误是类型错误的值没有成员“代码”。我该如何解决这个问题?
答案 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
。
code
是NSError