Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Functions and Optionals Parameters and Tuples Tuples

Whats the difference between returning a tuple and array

I cant get the difference between returning a tuple and an array. In both cases i return multiple values. And if i want to use the key value thing i can just use any collection like a dictionary. so Whats its edge ?

2 Answers

Eric Gu
Eric Gu
3,871 Points

With tuples, you have the ability to return a collection type consisting of different data types.

With arrays, all the items in the collection type must be of the same data type.

Akilah Jones
Akilah Jones
18,397 Points

Also using tuples is more efficient...

(value1, value2, value3) = functionThatReturnsTuple

value = functionThatReturnsArray

value1 = value[0];

value2 = value[1];

value3 = value[2];

Samar Khanna
Samar Khanna
1,757 Points

i didn't get what u said

Akilah Jones
Akilah Jones
18,397 Points

Samar Khanna what I was trying to demonstrate is the difference in using a tuple to store data versus using an array to store data...

When using a tuple, as shown above, it only takes one line

(value1, value2, value3) = functionThatReturnsTuple

Where as using an array, to do the exact same thing, takes four lines

value = functionThatReturnsArray

value1 = value[0];

value2 = value[1];

value3 = value[2];

I hope that helps

Akilah Jones
Akilah Jones
18,397 Points

Samar Khanna what I was trying to demonstrate is the difference in using a tuple to store data versus using an array to store data...

When using a tuple, as shown above, it only takes one line

(value1, value2, value3) = functionThatReturnsTuple

Where as using an array, to do the exact same thing, takes four lines

value = functionThatReturnsArray

value1 = value[0];

value2 = value[1];

value3 = value[2];

I hope that helps