Reputation: 179
I was watching Stanford - Developing iOS 9 Apps with Swift - 3. More Swift and Foundation Framework on youtube was talking about tuples and lists and i'm not sure what they are even if i searched online i still can't understand what they are.
Upvotes: 0
Views: 171
Reputation: 579
Turple is basically a group of variables, while a list is an index of a few values of the same type.
Tuple
Tuples are really cool (I think). It lets you basically put a bunch of variables together and put a name to them or nothing.
For example, let's say you have the quadratic formula. It would make sense to group these values:
var quadraticFunction: (a: Int, b: Int, c: Int, plus: Double, minus: Double)
With this you can even return the tuple. To take this further, you might want to make a function that performs the quadratic formula by taking 1 number and adding 1 for b and 2 for c.
func performQuadraticFormula(startValue: Int) -> (a: Int, b: Int, c: Int, plus: Double, minus: Double) {
var returnQuadratic: (a: Int, b: Int, c: Int, plus: Double, minus: Double)
returnQuadratic.a = startValue
returnQuadratic.b = startValue + 1
returnQuadratic.c = startValue + 2
returnQuadratic.plus = /* lots of math for this part */
returnQuadratic.minus = /* lots of math for this part */
return returnQuadratic
}
So now you can take this output and actually store it for use later:
let function = performQuadraticFormula(startValue: 10)
print(function.a)
// you can do whatever you want with this output now
So you can assign names to these values, store them for later, assign them, return them in a function. Also, you don't have to have a name for each value, in which case it would be like this:
function.0
Lists
I guess I can see how you can easily get confused with a list and a tuple. Both can be referenced with the index of it (tuple.0, list[0]). But lists are pretty different. First off, you cannot assign names for each value. The real big difference though, is a tuple is predefined with its values while a list can expand and remove items easily.
For example, you can store a few tests grades in a list
var tests = [100, 100, 90, 78, 100, 10]
Then, next week go and add a new one and it will automatically expand:
tests.append(99.8)
A tuple migght only have 3 values and that's it; you would have to go back and add a new variable in order to add a new value. For a list you have a bunch of the same type and can add whatever you want. To get one you can do this:
let firstTest = tests[0]
Also, a list (Array) has many functions that come with it like .map(), filter(), etc. Tuple does not have that.
Think of a list of a group of statistics or data and a tuple as just a way to put a bunch of variables on one line
Upvotes: 2
Reputation: 480
A tuple is a group of zero or more values represented as one value.
For example ("John", "Smith")
holds the first and last name of a person. You can access the inner values using the dot(.) notation followed by the index of the value:
var person = ("John", "Smith")
var firstName = person.0 // John
var lastName = person.1 // Smith
An array literal is written as a list of values, separated by commas, surrounded by a pair of square brackets:
The example below creates an array called shoppingList to store String values:
var shoppingList: [String] = ["Eggs", "Milk"]
// shoppingList has been initialized with two initial items
var firstItem = shoppingList[0]
// firstItem is equal to "Eggs"
Upvotes: 2