[英]Type of expression is ambiguous without more context in Swift 3
我正在尝试学习Swift,并正在阅读有关推送通知的教程。
let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge , .Sound], categories: nil)
给我错误
“没有更多的上下文,表达的类型是模糊的”。
我直接从教程中复制/粘贴了这一行,并在StackOverFlow上找到了相同的行。
我究竟做错了什么?
我正在使用Xcode 8。
查看UIUserNotificationSettings
的文档。 它的签名在Swift 3中已经改变,因为它具有类型的值。
你需要:
let settings = UIUserNotificationSettings(types: [.alert, .badge , .sound], categories: nil)
当然,如果您只支持iOS 10及更高版本,那么您根本不应该使用UIUserNotificationSettings
,因为它现在已被弃用。 请改用UNNotificationSettings
。 但是,如果您仍然支持iOS 9或更早版本,那么只要您更改为更新的语法,就可以使用UIUserNotificationSettings
。
如果要实现UNNotificationSettings
,则在iOS 10中使用UIUserNotificationSettings
不推荐使用UNNotificationSettings
,如下所示。
首先,您需要导入UserNotifications
。
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge , .sound]) {
(granted, error) in
}
有关更多细节检查本教程由米哈尔Kałużny UserNotifications.framework
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.