Collections and Control Flow: Arrays

OK, last entry for today! A nice problem to have, for sure - and quite a contrast to the past few months where my entries were so rare. Of course, I'm not going to hide from the fact that I've been much keener because the content is easier and more familiar, etc etc. My point is that I don't want to pack TOO much in at once, as the information will not be retained or made as much sense of. So, starting with 'Collections and Control Flow', the first part that I'm going to focus on is Arrays. There are several videos ahead, so will work through one at a time.

Introduction to Arrays

Pasan starts with a brief recap that we've covered variables, constants etc. In programming, we often like to work with multiple values. So far, we've focused on simple objects - whether it be strings, ints etc. There are three collection types in Swift:


  • Arrays
  • Dictionaries
  • Sets


This course will focus on the first two. In an Array, a number is associated with each item - from 0 to 1, 2, 3 etc. This means it is ordered. Formal definition - ordered list of values.

var toDo = ["Do shopping", "Target Tracker", "Watch F1"]

This is how it looks - the syntax is using the  box/square brackets. Simple in the example above - a string array. This is familiar! I know that you can't mix types within an array and that you can adjust and change/add to an array as well. 

You can also start an empty array; in that case I think you have to declare the type. Pasan goes on to explain that you cannot mix types - this links to type safety - the compiler will expect that the string array example will ONLY have strings!

Adding items to Arrays

As I thought, it is the 'append' keyword that is used to add an item...

toDo.append("Cook dinner")

Appending will put the item onto the end of the array! That's straightforward enough. I know that you can them in other ways too. You can use concatenation to add onto an array of e.g. numbers. 

toDo += ["Clean the oven"]

Now this is combing the unary addition operator with arrays - this is another way of appending basically. Another key point is that you can't make the array a constant, if you then want to add to it/change it. You can only do so if it is a variable (mutable).

Modifying items in an Array

The first aspect of this is extricating from the Array. So for the above example, the square brackets must be used, with the index system starting from 0. Changing the value would be straightforward enough too. Anyway, here is creating a constant with info from the array. 

let firstTask = toDo[0]

Mutating an array - this means to change the array in some way. 

toDo[1] = "Sweep up"

In this case, the second item on the original list is now replaced. Simple!

Inserting and Deleting Values

toDo.insert("Polish the tables", at: 3)

The inbuilt method of Xcode (are these properties or parameters?) has the option for what to add, and at what index - so this is definitely a method, for inserting a new item at a specific place on the array. 

toDo.remove(at: 2)

Easy enough for removing items in an array - again, Xcode has a method for this already. All I needed to type was the .remove, then the rest came up for me. Removing an item does then change the index number of all subsequent items. The point Pasan next makes is that when you have millions of items in an array, you have to be careful when deleting items. The larger the array, the more 'expensive' these types of operations are.

In addition, the .count property (another method?) is used to find out the number of items in an array. You can't select an item that is not there e.g. selecting [6] in an array of less than 7 items. 

And that's everything on Arrays! Next time we will be looking at Dictionaries. Now, I remember these being an unordered collection, which need a key-value pair. So far, nothing has phased me, which again is lovely; the real test is when I learn new/go over tricky info from before that I'll really need to get my head around. 

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)