How can I merge two arrays into a dictionary?

前端 未结 6 1752
青春惊慌失措
青春惊慌失措 2020-12-16 14:39

I have 2 arrays:

    var identic = [String]()
    var linef = [String]()

I\'ve appended them with data. Now for usability purposes my goal

6条回答
  •  没有蜡笔的小新
    2020-12-16 15:10

    A slightly different method, which doesn't require the arrays to be of the same length, because the zip function will safely handle that.

    extension Dictionary {
        init(keys: [Key], values: [Value]) {
            self.init()
    
            for (key, value) in zip(keys, values) {
                self[key] = value
            }
        }
    }
    

提交回复
热议问题