深层嵌套数据结构无法下标错误

时间:2015-11-01 05:45:20

标签: ios arrays swift dictionary

我有一个深层嵌套的字典数组:

func markAllBooksAsUnread() {

    for (cat, catArray) in bookage.setless {
        for (gen, genArray) in catArray {
            for (foc, focArray) in genArray {
                for dict in focArray {
                    bookage.setless[cat]![gen]![foc]![dict]["read"] = "no"
                }
            }
        }
    }
}

这段代码看起来对我来说正在产生错误:

Cannot subscript a value of type Array<Dictionary<String, String>> with an index of type 'Dictionary<String, String>'

将胡萝卜放在[dict]中的'd'下。 bookage.setless数组的结构是:

var setless = [String : Dictionary<String, Dictionary<String, Array<Dictionary<String, String>>>>]()

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

dict是一个字典,你不能将它用作索引。试试这个:

for (cat, catArray) in bookage.setless {
    for (gen, genArray) in catArray {
        for (foc, focArray) in genArray {
            for i in focArray.indices {
                bookage.setless[cat]![gen]![foc]![i]["read"] = "no"
            }
        }
    }
}