Ordered map in Swift

前端 未结 12 1169
忘掉有多难
忘掉有多难 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:16

    Here's what I did, pretty straightforward:

    let array = [
        ["foo": "bar"],
        ["foo": "bar"],
        ["foo": "bar"],
        ["foo": "bar"],
        ["foo": "bar"],
        ["foo": "bar"]
    ]
    
    // usage
    for item in array {
        let key = item.keys.first!
        let value = item.values.first!
    
        print(key, value)
    }
    

    Keys aren't unique as this isn't a Dictionary but an Array but you can use the array keys.

提交回复
热议问题