无法转换类型' [AnyObject]'的值预期的参数类型' [UIUserNotificationAction]?'

时间:2016-10-31 02:15:33

标签: swift

var category:UIMutableUserNotificationCategory=UIMutableUserNotificationCategory()
category.identifier="DEFAULT_GATEGORY"
let defaultAction:NSArray=[action]
category.setActions(defaultAction as [AnyObject] as [AnyObject], forContext: UIUserNotificationActionContext.Minimal)

1 个答案:

答案 0 :(得分:0)

假设action的类型为UIUserNotificationAction

var category = UIMutableUserNotificationCategory() // NOTE: type not needed.
category.identifier = "DEFAULT_GATEGORY"
let defaultAction = [action] // NOTE: Type not needed.
category.setActions(defaultAction, forContext: UIUserNotificationActionContext.Minimal)

正如您所看到的,我所做的就是删除不需要的(和误导性的)类型信息。我发现依靠Swift的自动类型确定系统很有帮助。

使用以下Playground代码进行测试:

let action = UIMutableUserNotificationAction()
var category = UIMutableUserNotificationCategory()
category.identifier = "DEFAULT_GATEGORY"
let defaultAction = [action]
category.setActions(defaultAction, forContext: UIUserNotificationActionContext.Minimal)