字典[String:String]转换为数组swift时,键顺序已更改

编程入门 行业动态 更新时间:2024-10-12 01:22:33
本文介绍了字典[String:String]转换为数组swift时,键顺序已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有如下字典,

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时,键顺序已更改

本文发布于:2023-11-26 20:31:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635088.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   字典   顺序   String

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!