2016-10-11 54 views
3

我努力學習斯威夫特並打算通過推送通知的教程更多的上下文曖昧。類型的表達是沒有斯威夫特3

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

是給我的錯誤

「表達的類型是沒有更多的背景下曖昧」。

我直接從教程複製/粘貼這一行,並發現這條線在StackOverFlow上使用。

我在做什麼錯?
我使用的Xcode 8

+0

依我看由米哈爾Kałużny檢查這個tutorial此獸是那邊? ;)http://balancedcode.com/blog/files/de01930f6f2cf9e4c886531eca028cba-0.html我已經受夠了Xcode的線索來解決它彌補斯威夫特3. –

回答

11

看爲UIUserNotificationSettings的文檔。它的簽名在Swift 3中已經改變,類型的值也一樣。

您需要:

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

當然,如果你只支持iOS的10及更高版本,那麼你不應該使用UIUserNotificationSettings可言,因爲它現在已經過時。改爲使用UNNotificationSettings。但是,如果您仍然支持iOS 9或更早版本,那麼只要您更改爲更新後的語法,就可以使用UIUserNotificationSettings

+0

非常感謝,馬迪。我知道,ios的10和更高版本應該使用UNNotificationSettings類,但我剛起步,是唯一能夠找到的ios 9.所以我打算從那裏開始一個手拿着教程。 –

1

UIUserNotificationSettings在iOS 10中不推薦使用UNNotificationSettings,如果您想實施UNNotificationSettings,則執行如下所示。

首先,你需要導入UserNotifications了點。

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

} 

,詳細瞭解這對UserNotifications.framework