Swift 3 中没有更多上下文的表达式类型不明确

Type of expression is ambiguous without more context in Swift 3

我正在努力学习 Swift 并且正在阅读有关推送通知的教程。

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: nil)

给我错误

"Type of expression is ambiguous without more context".

我 copy/pasted 这一行直接来自教程,并且发现在 Whosebug 上使用了同一行。

我做错了什么?
我是使用 Xcode 8.

查看 UIUserNotificationSettings 的文档。它的签名在 Swift 3 中发生了变化,类型的值也发生了变化。

你需要:

let settings = UIUserNotificationSettings(types: [.alert, .badge , .sound], categories: nil)

当然,如果您只支持 iOS 10 及更高版本,那么您根本不应该使用 UIUserNotificationSettings,因为它现在已被弃用。请改用 UNNotificationSettings。但是,如果您仍然支持 iOS 9 或更早版本,那么只要更改为更新的语法,使用 UIUserNotificationSettings 就可以了。

UIUserNotificationSettings 在 iOS 10 中被弃用 UNNotificationSettings,如果你想实现 UNNotificationSettings 然后像下面那样实现。

首先您需要为此导入 UserNotifications

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge , .sound]) {
     (granted, error) in

}

有关此内容的更多详细信息,请查看 Michał Kałużny 在 UserNotifications.framework

上撰写的 tutorial