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.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
George B
  • 2,592
  • 3
  • 22
  • 27
  • Methinks this beast be yonder? ;) http://balancedcode.com/blog/files/de01930f6f2cf9e4c886531eca028cba-0.html I've had to work it up with xCode's cues for Swift 3. – Alex Hall Feb 28 '17 at 04:10

2 Answers2

11

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks a lot, maddy. I am aware that ios 10 and later should use the UNNotificationSettings class, but I am just getting started and was only able to find a hand-holding tutorial for ios 9. So I am going to start from there. – George B Oct 11 '16 at 05:21
1

UIUserNotificationSettings is deprecated with UNNotificationSettings in iOS 10, if you want to implement UNNotificationSettings then implement like below.

First you need to import UserNotifications for that.

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

}

For more details on this check this tutorial by Michał Kałużny on UserNotifications.framework

Nirav D
  • 71,513
  • 12
  • 161
  • 183