George B George B - 1 year ago 155
iOS Question

Type of expression is ambiguous without more context in Swift 3

I am trying to learn swift and am going through a tutorial on push notifications.

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

Is giving me the error "Type of expression is ambiguous without more context".
I copy/pasted this line directly from a tutorial and have found this same line used on StackOverFlow.

What am I doing wrong?
I am using Xcode 8.

Answer Source

Look at the documentation for UIUserNotificationSettings. Its signature has changed in Swift 3 as has the values for the types.

You need:

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

Of course if you are only supporting iOS 10 and later, then you shouldn't use UIUserNotificationSettings at all since it is now deprecated. Use UNNotificationSettings instead. But if you are still supporting iOS 9 or earlier, then using UIUserNotificationSettings is fine as long as you change to the updated syntax.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download