Ordered map in Swift

前端 未结 12 1221
忘掉有多难
忘掉有多难 2020-11-28 09:24

Is there any built-in way to create an ordered map in Swift 2? Arrays [T] are sorted by the order that objects are appended to it, but dictionaries

12条回答
  •  误落风尘
    2020-11-28 10:23

    As Matt says, dictionaries (and sets) are unordered collections in Swift (and in Objective-C). This is by design.

    If you want you can create an array of your dictionary's keys and sort that into any order you want, and then use it to fetch items from your dictionary.

    NSDictionary has a method allKeys that gives you all the keys of your dictionary in an array. I seem to remember something similar for Swift Dictionary objects, but I'm not sure. I'm still learning the nuances of Swift.

    EDIT:

    For Swift Dictionaries it's someDictionary.keys

提交回复
热议问题