How do I add a tuple to a Swift Array?

后端 未结 8 574
夕颜
夕颜 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:40

    I've ended up here multiple times due to this issue. Still not as easy as i'd like with appending onto an array of tuples. Here is an example of how I do it now.

    Set an alias for the Tuple - key point

    typealias RegionDetail = (regionName:String, constraintDetails:[String:String]?)
    

    Empty array

    var allRegionDetails = [RegionDetail]()
    

    Easy to add now

    var newRegion = RegionDetail(newRegionName, constraints)
    allRegionDetails.append(newRegion)
    
    var anotherNewRegion = RegionDetail("Empty Thing", nil)
    allRegionDetails.append(anotherNewRegion)
    

提交回复
热议问题