Type Any has no subscript members Error in Swift 3

2019-09-09 01:39发布

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条回答
孤傲高冷的网名
2楼-- · 2019-09-09 02:30

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

let items = Array(todoDictionary.values) as! [[String: Any]]
查看更多
登录 后发表回答