Ordered Dictionary in JSON

前端 未结 4 1723
梦毁少年i
梦毁少年i 2020-12-21 16:28

There are 3 string variables

public var userLoginId : String?
public var searchString : String?
public var tableName : String?

I have a dic

4条回答
  •  执念已碎
    2020-12-21 16:40

    Dictionaries are "unordered collections". They do not have any order at all to their key/value pairs. Period.

    If you want an ordered collection, use something other than a dictionary. (an array of single-item dictionaries is one way to do it.) You can also write code that loads a dictionary's keys into a mutable array, sorts the array, then uses the sorted array of keys to fetch key/value pairs in the desired order.

    You could also create your own collection type that uses strings as indexes and keeps the items in sorted order. Swift makes that straightforward, although it would be computationally expensive.

提交回复
热议问题