0

Apologies in advance. I am a newbie learning Swift and have some working experience with C and Ruby. I'm venturing into Swift.

I'm trying to create and access a multi-dimensional array as follows:

var arrayTest: [[(Int, Int, Int, String)]] = []
arrayTest.append([(1, 2, 3, "Test")])
arrayTest.append([(4, 5, 6, "Test1")])

This seems to work fine in Playground. But trying to access as follows does not work:

println(arrayTest[0][0])

I looked at several entries and the Apple Swift docs. But I was still unable to figure this out.

Any insights appreciated. Thank you.

1 Answer 1

1

Looks like you're trying to make an array of tuples but have an extra set of brackets, meaning you're making an array of arrays of tuples. Also, you append a tuple without putting it in parenthesis. Try:

var arrayTest: [(Int, Int, Int, String)] = []
arrayTest.append(1, 2, 3, "Test")
arrayTest.append(4, 5, 6, "Test1")

You would access an element a little differently. Square brackets return the tuple in arrayTest, and the period returns an element of the tuple.

println(arrayTest[0].0)

This would return 1 (the 0th element of the 0th tuple).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.