【发布时间】:2018-02-02 02:59:13
【问题描述】:
我有一本根据用户选择的选项而变化的字典。例如:
[“Item1”:7,“Item2”:4,“Item3”:4,“Item4”:7,“Item5”:6]
每个项目旁边的数字是每个项目的计数。我想知道如何将这个字典转换成表格?那么左列是项目,右列是项目计数?
最好的方法是什么?
数据原来是一个数组,格式为:["Item1", "Item2", "Item3", "Item1", "Item1", "Item2"]。
但是我用过
var counts: [String: Int] = [:] , myArray.forEach { counts[$0, default: 0] += 1 }
计算数组中的每一项
【问题讨论】:
-
什么数组?到目前为止,您已经发布了一本字典。
-
对不起,改成字典了。
-
表格使用什么顺序?字典是无序的。
-
嗯,我需要考虑一下。数据原本是一个数组,格式为:["Item1", "Item2", "Item3", "Item1", "Item1", "Item2"。但我使用 var counts: [String: Int] = [:] , myArray.forEach { counts[$0, default: 0] += 1 } 来计算数组中的每个项目,然后将其放入字典中
标签: swift uitableview dictionary swift4