Type Any has no subscript members Error in Swift 3.0?

六眼飞鱼酱① 提交于 2019-12-25 07:18:04

问题


I am following this tutorial here...

And the issue I am having is I keep getting the error.

"Type Any has no subscript members Error" in this function...

func allItems() -> [TodoItem] {
let todoDictionary = NSUserDefaults.standardUserDefaults().dictionaryForKey(ITEMS_KEY) ?? [:]
let items = Array(todoDictionary.values)
return items.map({TodoItem(deadline: $0["deadline"] as! NSDate, title: $0["title"] as! String, UUID: $0["UUID"] as! String!)}).sort({(left: TodoItem, right:TodoItem) -> Bool in
        (left.deadline.compare(right.deadline) == .OrderedAscending)
}

The error is being generated on this line...

return items.map({TodoItem(deadline: $0["deadline"] as! NSDate, title: $0["title"] as! String, UUID: $0["UUID"] as! String!)}).sort({(left: TodoItem, right:TodoItem) -> Bool in
        (left.deadline.compare(right.deadline) == .OrderedAscending)}

I am completely stumped.

Any help would be appreciated! Thank you!


回答1:


You need to explicitly specify the type of items object as [[String:Any]].

let items = Array(todoDictionary.values) as! [[String: Any]]


来源:https://stackoverflow.com/questions/39553834/type-any-has-no-subscript-members-error-in-swift-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!