How do I create an array of tuples?
Asked Answered
C

6

12

I'm trying to create an array of tuples in Swift, but having great difficulty:

var fun: (num1: Int, num2: Int)[] = (num1: Int, num2: Int)[]()

The above causes a compiler error.

Why's that wrong? The following works correctly:

var foo: Int[] = Int[]()
Colostomy answered 2/7, 2014 at 19:40 Comment(1)
possible duplicate of Array of tuples in SwiftNeonatal
C
24

It works with a type alias:

typealias mytuple = (num1: Int, num2: Int)

var fun: mytuple[] = mytuple[]()
// Or just: var fun = mytuple[]()
fun.append((1,2))
fun.append((3,4))

println(fun)
// [(1, 2), (3, 4)]

Update: As of Xcode 6 Beta 3, the array syntax has changed:

var fun: [mytuple] = [mytuple]()
// Or just: var fun = [mytuple]()
Caretaker answered 2/7, 2014 at 19:45 Comment(2)
This has changed for Swift - Beta 3.Matabele
@vacawama: You are right (but the old syntax still works, just causing a compiler warning). I have updated the answer accordingly. Thanks for the feedback.Caretaker
C
10

You can do this, just your assignment is overly complicated:

var tupleArray: [(num1: Int, num2: Int)] = [ (21, 23) ]

or to make an empty one:

var tupleArray: [(num1: Int, num2: Int)] = []
tupleArray += (1, 2)
println(tupleArray[0].num1)    // prints 1
Conduit answered 2/7, 2014 at 20:1 Comment(4)
You are right! var tupleArray: (num1: Int, num2: Int)[] = [] just works.Caretaker
Yeah, tuples and arrays are seriously quirky. This Q&A has more strangeness: https://mcmap.net/q/811626/-array-of-tuples-in-swiftConduit
This has changed for Swift - Beta 3.Matabele
@Matabele Yeah, not sure I want to go through all my answers and update the syntax, but I feel bad having invalid code out there...Conduit
M
3

This also works:

var fun:Array<(Int,Int)> = []
fun += (1,2)
fun += (3,4)

Oddly though, append wants just one set of parens:

fun.append(5,6)

If you want the labels for the tuple parts:

var fun:Array<(num1: Int, num2: Int)> = []
fun += (1,2)                  // This still works
fun.append(3,4)               // This does not work
fun.append(num1: 3, num2: 4)  // but this does work
Matabele answered 2/7, 2014 at 20:14 Comment(1)
This is a bug/feature in Swift with arrays of tuples. More here: https://mcmap.net/q/811626/-array-of-tuples-in-swiftConduit
G
1

Not sure about earlier versions of Swift, but this works in Swift 3 when you want to provide initial values:

var values: [(num1: Int, num2: Int)] = {
    var values = [(num1: Int, num2: Int)]()
    for i in 0..<10 {
        values.append((num1: 0, num2: 0))
    }
    return values
}()
Gattis answered 19/4, 2017 at 2:58 Comment(0)
M
0

As of Swift 5.7, the following works.

  1. Creating an empty array that you can populate later, using the standard array initialiser with the tuple's type signature.
/* Create empty array of (num1: Int, num2: Int) tuples */
var array1 = [(num1: Int, num2: Int)]()

/* Can be populated with (Int, Int) tuples
   (the labels are optional)... */
for i in 1...8 {
  array1.append((i, i*i))
}

/* ...or (num1: Int, num1: Int) tuples */
for i in 9...12 {
  array1.append((num1: i, num2: i*i))
}
  1. Creating an initialised array using Array(repeating: <value>, count:):
var array2 = Array(repeating: (num1: 0, num2: 0), count: 8)
Methane answered 13/12, 2022 at 10:1 Comment(0)
P
-2

It can be done like this as well.

      var arr : [(num1: Int, num2 : Int)] = {
          let arr =  Array(repeating: (num1:  0, num2 :0), count : n)
          return arr
     }()  
Progenitive answered 7/12, 2022 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.