How do I add a tuple to a Swift Array?

后端 未结 8 494
夕颜
夕颜 2021-01-30 03:02

I\'m trying to add a tuple (e.g., 2-item tuple) to an array.

var myStringArray: (String,Int)[]? = nil
myStringArray += (\"One\", 1)

What I\'m g

8条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 03:36

     @assignment func += (inout left: Array<(String, Int)>?, right: (String, Int)) {
        if !left {
            left = []
        }   
        if left {
            var array = left!
            array.append(right.0, right.1)
            left = array
        }
    }
    
    var myStringArray: (String,Int)[]? = nil
    myStringArray += ("x",1)
    

提交回复
热议问题