Swift2 NotificationType Option Error

972 views Asked by At

After mutating Swift1.2 to Swift2 have an error... have no idea about how to fix it, smb tried this in Swift2?

func application(application: UIApplication, 
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) ->        Bool {

        let notificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
        let settings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
        application.registerUserNotificationSettings(settings)
        return true
            }

EDIT:

Error: "Cannot find initialiser for type 'UIUserNotificationSettings' that accept an argument list of type '(forTypes:[UIUserNotifivati..."

2

There are 2 answers

0
iEmad On BEST ANSWER

Simply change this part from your code

let notificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]

INTO:

let notificationType: UIUserNotificationType = [.Alert, .Badge, .Sound]
1
zrzka On

Wonder why people don't read release notes or documentation (UIUserNotificationType, OptionSetType).


Here's the release notes excerpt:

NS_OPTIONS types get imported as conforming to the OptionSetType protocol, which presents a set-like interface for options. (18069205) Instead of using bitwise operations such as:

// Swift 1.2:
object.invokeMethodWithOptions(.OptionA | .OptionB)
object.invokeMethodWithOptions(nil)
if options & .OptionC == .OptionC {
  // .OptionC is set
}

Option sets support set literal syntax, and set-like methods such as contains:

object.invokeMethodWithOptions([.OptionA, .OptionB])
object.invokeMethodWithOptions([])
if options.contains(.OptionC) {
  // .OptionC is set
}

A new option set type can be written in Swift as a struct that conforms to the OptionSetType protocol. If the type specifies a rawValue property and option constants as static let constants, the standard library will provide default implementations of the rest of the option set API:

struct MyOptions: OptionSetType {
  let rawValue: Int
  static let TuringMachine  = MyOptions(rawValue: 1)
  static let LambdaCalculus = MyOptions(rawValue: 2)
  static let VonNeumann     = MyOptions(rawValue: 4)
}
let churchTuring: MyOptions = [.TuringMachine, .LambdaCalculus]

As @iEmad wrote, just change one line of your code to:

let notificationType: UIUserNotificationType = [.Alert, .Badge, .Sound]

How can you find this yourself? Error is ...

Error: "Cannot find initialiser for type 'UIUserNotificationSettings' that accept an argument list of type ...

... which basically says that you're passing invalid arguments to initializer. What are correct arguments? Again, documentation:

convenience init(forTypes types: UIUserNotificationType,
      categories categories: Set<UIUserNotificationCategory>?)

Lets start with the last one - categories. You're passing nil, which is okay, because categories type is Set<UIUserNotificationCategory>? = optional = nil is okay.

So, the problem is with the first one. Back to documentation where we can find UIUserNotificationType declaration:

struct UIUserNotificationType : OptionSetType {
    init(rawValue rawValue: UInt)
    static var None: UIUserNotificationType { get }
    static var Badge: UIUserNotificationType { get }
    static var Sound: UIUserNotificationType { get }
    static var Alert: UIUserNotificationType { get }
}

Hmm, it adopts OptionSetType. Didn't see this in Swift 1.2, it must be something new. Let's open documentation and learn more about it. Ahh, interesting, nice, I have to adapt my code.

Please, start reading release notes and documentation. You're going to save some time.