类型任何没有下标成员在Swift 3.0中的错误? [英] Type Any has no subscript members Error in Swift 3.0?

查看:143
本文介绍了类型任何没有下标成员在Swift 3.0中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这个这里的教程 ...

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)}

我完全陷入困境。

任何帮助将不胜感激!谢谢!

Any help would be appreciated! Thank you!

推荐答案

您需要明确指定类型的项目对象为 [[String:Any]]

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

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

这篇关于类型任何没有下标成员在Swift 3.0中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆