user5473123
user5473123

Reputation: 21

Swift 2(.1) migration error

I updated my Swift 1.2 Project to Swift 2.1 (installing Xcode 7.1). Now I get 2 errors.

1st error:

Downcast from 'NSURL?' to 'NSURL' only unwraps optionals; did you mean to use '!'?

in this line of code:

let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last as! NSURL

2nd error:

Cannot convert value of type 'Set<NSObject>' to expected argument type 'Set<UIUserNotificationCategory>?'

In this line of code:

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: NSSet(array: [todoCategory]) as! Set<NSObject> as Set<NSObject>))

Do you know how to fix it?

Upvotes: 0

Views: 370

Answers (1)

joern
joern

Reputation: 27620

The first error is fixed when you remove the casting. However who probably want to check if documentDirectory is not nil so you would put it in a if let:

if let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last {
    // do something with the documents directory
}

The second error can be fixed like this:

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: Set<UIUserNotificationCategory>.init(arrayLiteral: todoCategory)))

Upvotes: 1

Related Questions