Swift 2(.1)迁移错误

时间:2015-10-28 16:38:06

标签: migration swift2 xcode7 swift2.1

我将Swift 1.2项目更新为Swift 2.1(安装Xcode 7.1)。现在我得到2个错误。

第一个错误:

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

在这行代码中:

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

第二次错误:

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

在这行代码中:

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

你知道怎么解决吗?

1 个答案:

答案 0 :(得分:1)

删除转换时,第一个错误已修复。但是,谁可能想检查documentDirectory是否为nil所以你要把它放在if let中:

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

第二个错误可以修复如下:

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