57

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 getting is:

Could not find an overload for '+=' that accepts the supplied arguments


Hint: I tried to do an overload of the '+=' per reference book:

@assignment func += (inout left: (String,Int)[], right: (String,Int)[]) {
    left = (left:String+right:String, left:Int+right+Int)
}

...but haven't got it right.

Any ideas? ...solution?

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105

8 Answers8

141

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))
Rémy Virin
  • 3,379
  • 23
  • 42
Mike MacMillan
  • 1,575
  • 1
  • 11
  • 7
  • The 'append' trick makes the code looks cleaner, but also more confusing... Any logic behind it except for code-cleanliness? – Aviel Gross Jan 12 '15 at 20:56
  • append() feels more useful in cases where i am adding one or a few objects, for code readability...the compound assignment operator, or initializing the array with a set of data, are better adapted for adding a large number of objects at once. – Mike MacMillan Jan 14 '15 at 20:31
  • 2
    This gives me trouble in Swift 2.0 - ```Extra argument 'value' in call``` – MrBr Jul 22 '15 at 14:15
  • 1
    @MrBr Try adding parenthesis such as `stuff.append((name: "test 2", value: 2))` – Elsint Sep 26 '15 at 07:15
14

You have two issues. First problem, you're not creating an "array of tuples", you're creating an "optional array of tuples". To fix that, change this line:

var myStringArray: (String,Int)[]? = nil

to:

var myStringArray: (String,Int)[]

Second, you're creating a variable, but not giving it a value. You have to create a new array and assign it to the variable. To fix that, add this line after the first one:

myStringArray = []

...or you can just change the first line to this:

var myStringArray: (String,Int)[] = []

After that, this line works fine and you don't have to worry about overloading operators or other craziness. You're done!

myStringArray += ("One", 1)

Here's the complete solution. A whopping two lines and one wasn't even changed:

var myStringArray: (String,Int)[] = []
myStringArray += ("One", 1)
Alvin Thompson
  • 5,388
  • 3
  • 26
  • 39
  • 3
    Of the answers, this one is the simplest and makes the most sense. Overriding operators is something that should rarely be done because those reading the code later will say "WTF". At least in this case it is in the range of the expected over-loadings. – zaph Jun 15 '14 at 20:12
  • 1
    @Zaph: Yep. Everyone went that route because the asker did. But of course, the asker was confused. That's why he was asking. :) – Alvin Thompson Jun 15 '14 at 20:16
6

Swift 4 solution:

// init empty tuple array
var myTupleArray: [(String, Int)] = []

// append a value
myTupleArray.append(("One", 1))
budiDino
  • 13,044
  • 8
  • 95
  • 91
3

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)
Logan
  • 52,262
  • 20
  • 99
  • 128
3

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)
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
1

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

Yank
  • 718
  • 6
  • 17
0
 @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)
Christian Dietrich
  • 11,778
  • 4
  • 24
  • 32
0

Thanks to comments:

import UIKit

@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
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let interestingNumbers = [
            "Prime": [2, 3, 5, 7, 11, 13],
            "Fibonacci": [1, 1, 2, 3, 5, 8],
            "Square": [1, 4, 9, 16, 25],
        ]

        println("interestingNumbers: \(interestingNumbers)\n")
        var largest = 0

        var myStringArray: (String,Int)[]? = nil

        myStringArray += ("One", 1)

        var x = 0

        for (kind, numbers) in interestingNumbers {
            println(kind)
            for number in numbers {
                if number > largest {
                    largest = number
                }
                x++
                println("\(x)) Number: \(number)")
                myStringArray += (kind,number)
            } // end Number
        } // end Kind
        println("myStringArray: \(myStringArray)")
    }
}

The Output:

interestingNumbers: [Square: [1, 4, 9, 16, 25], Prime: [2, 3, 5, 7, 11, 13], Fibonacci: [1, 1, 2, 3, 5, 8]]

Square
1) Number: 1
2) Number: 4
3) Number: 9
4) Number: 16
5) Number: 25
Prime
6) Number: 2
7) Number: 3
8) Number: 5
9) Number: 7
10) Number: 11
11) Number: 13
Fibonacci
12) Number: 1
13) Number: 1
14) Number: 2
15) Number: 3
16) Number: 5
17) Number: 8



Array of tupules:

myStringArray: [(One, 1), (Square, 1), (Square, 4), (Square, 9), (Square, 16), (Square, 25), (Prime, 2), (Prime, 3), (Prime, 5), (Prime, 7), (Prime, 11), (Prime, 13), (Fibonacci, 1), (Fibonacci, 1), (Fibonacci, 2), (Fibonacci, 3), (Fibonacci, 5), (Fibonacci, 8)]

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • That still doesn't work. If you try to run it, it will generate a runtime error (if it even compiles) because you never assign an actual value to `myStringArray `. `nil` does not mean "an empty array". `[]` does. See my answer. – Alvin Thompson Jun 15 '14 at 20:10
  • This is not an array of tuples but dictionaries. – Shmidt Dec 09 '14 at 15:57