How do I add a tuple to a Swift Array?

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

    Since this is still the top answer on google for adding tuples to an array, its worth noting that things have changed slightly in the latest release. namely:

    when declaring/instantiating arrays; the type is now nested within the braces:

    var stuff:[(name: String, value: Int)] = []
    

    the compound assignment operator, +=, is now used for concatenating arrays; if adding a single item, it needs to be nested in an array:

    stuff += [(name: "test 1", value: 1)]
    

    it also worth noting that when using append() on an array containing named tuples, you can provide each property of the tuple you're adding as an argument to append():

    stuff.append((name: "test 2", value: 2))
    
    0 讨论(0)
  • 2021-01-30 03:35

    Note: It's not work anymore if you do:

    array += tuple 
    

    you will get error what you need is :

    array += [tuple]
    

    I think apple change to this representation because it's more logical

    0 讨论(0)
  • 2021-01-30 03:36

    If you remove the optional, it works fine, otherwise you'll have to do this:

    var myStringArray: (String,Int)[]? = nil
    
    if !myStringArray {
        myStringArray = []
    }
    
    var array = myStringArray!
    array += ("One", 1)
    myStringArray = array
    

    You can never append an empty array, so you'll have to initialize it at some point. You'll see in the overload operator below that we sort of lazy load it to make sure that it is never nil.

    You could condense this into a '+=' operator:

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

    Then call:

    var myStringArray: (String,Int)[]? = nil
    myStringArray += ("one", 1)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2021-01-30 03:48

    Swift 4 solution:

    // init empty tuple array
    var myTupleArray: [(String, Int)] = []
    
    // append a value
    myTupleArray.append(("One", 1))
    
    0 讨论(0)
提交回复
热议问题