我有如下字典,
var dataSource: [String: String] = ["FirstName": "Austin", "ListName": "Michael", "Address": "Street Address", "City": "Chennai"]我想在UITableView中填充这些值,所以我尝试将所有keys从Dictionary移到如下所示的Array,
I want to populate these values in a UITableView, so I tried to get all the keys from Dictionary to an Array like below,
let dataArray = Array(dataSource.keys)我得到的输出像[String]一样,
["LastName", "FirstName", "City", "Address"]问题是,键的顺序已更改,我希望array的顺序与dictionary相同.
The problem is, the order of the keys has changed, I want the array in the same order as dictionary has.
任何人都可以帮忙吗?
推荐答案使用纯字典作为tableview数据源是个坏主意.
Use plain dictionary as tableview datasource is bad idea.
但是Dictionary无法排序.因此,将键值添加到字典的顺序无关紧要.
However Dictionary can not be sorted. So it doesn't matter in what order you add your keys-values to the dictionary.
如果需要排序,请改用字典数组.
If you need sorted then use array of dictionary instead.
您应该使用模型而不是易于维护的普通字典:]
You should use models instead of plain dictionary that is easy to maintain :]
喜欢
struct User { var firstName:String? var lastName:String? var address:String? var city:String? }更多推荐
字典[String:String]转换为数组swift时,键顺序已更改
发布评论