Beginning Swift 3 - Part 11: Arrays | Ray Wenderlich

Learn about arrays in Swift 3.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4095-beginning-swift-3/lessons/11

hey there Brian, I am very new to programming and want to learn swift so I have been watching these videos as we go along and
I am really confused at this point and some things up to here I have just wanted to ask.
but in this video in the arrays you say if you want to add a Int or value to the array you have to use the .append keyword? I have noticed this with a few things, where there is a certain way to add something to the variable or something like that

but my question is if you want to add something to the array, why don’t you just go back to the top where you set the array and set it in? why do you have to write a whole new line to add something?

Hey Joey,

The reason you use the append keyword is because sometimes in your app you will not have the value there. The reason why the append keyword is so useful is so that we can add values to our arrays at a later time. For example, lets say we are using an array to hold emails for an app. At first, you may not have any emails, but when users register, you will append the email to your array. Make sense? :grinning:

3 Likes

that makes so much sense, wow thank you (:

1 Like

No prob :slight_smile:

Hey Brian,Can you explain me the main difference between an Array and a Tuple besides their syntax?
Also,which one is used where?
I am really confused between them.

An Array is a Collection Type, whereas a Tuple is a Type.
What does that mean ?

According to the Collection Types description from Apple, the Array is made to store instances of other objects, and if assigned to a variable, can have its content changed at runtime.

The Tuple, in the other hand, store multiple values related to each other, but you can’t add or remove values in it : it’s like a struct or a class. The values can be of different types as stated in the Tuple description :

Tuples group multiple values into a single compound value. The values within a tuple can be of any type and do not have to be of the same type as each other.


Let’s have a simple example with a daily shopping cart. When you buy something, you have to specify the quantity.
So you can create a simplified tuple for things you want to buy :

let tomatoesItem   = (name:"Tomato", quantity:2)
let toothbrushItem = (name:"ToothBrush", quantity:1)

All these items put together create your shopping list, which is, in fact, just an array made of these tuples.

 var shoppingListArray = [tomatoesItem, toothbrushItem]

Since this is a variable array, you can add further tuple in it at runtime when you decide to add a new item on the fly. You can’t add new fields in a tuple at runtime.

let newItem = (name:"1L Milk", quantity:2)
shoppingListArray.append(newItem)
print(shoppingListArray)
// -> Prints [(name: "Tomato", quantity: 2), (name: "ToothBrush", quantity: 1), (name: "1L Milk", quantity: 2)]
1 Like

Thanks for the help !
Really appreciate it !! :slight_smile:

Thanks for HD video options. It was much needed feature.

Hello.

Can anyone explain to me why using ‘min’ on an array requires a set of empty parenthesis but using other things like ‘count’ and ‘first’ don’t?

For example, in the array tutorial video, Ray creates an array named evenNumbers:

evenNumbers = [2,4,6,8,10,12]

He determines the first element of this array using ‘first’:

evenNumbers.first

And He determines the number of elements in this array using ‘count’:

evenNumbers.count

But he determines the least value of an array using min followed by a set of empty parenthesis. Like this:

evenNumberV.min()

Why is the set of empty parenthesis needed after ‘min’ but not after ‘count’ or ‘first’?

Also, Ray refers to ‘first’ and ‘count’ as ‘pieces of data’ but refers to ‘min’ as a method. Why? I mean, I know what a method is in general, but why how do we know that ‘min’ is a method when it’s used with an array and ‘first’ and ‘count’ are pieces of day? How do we know that? And, does that have something to do with the empty parenthesis? How do you tell if something is a ‘piece of data’ vs. a ‘method’ on something like an array?

Thanks in advance…

Mary

@kenkelm Please check out my object oriented programming in Swift tutorial when you get a chance in order to understand the exact difference between a property and a method:

https://www.raywenderlich.com/160728/object-oriented-programming-swift

I hope it helps. Let me know if you have any other questions or issues about the whole thing. Thank you!