What I know about Swift - Part Two
I don't know how many parts I will need for explaining what I know about Swift - this may be the last one! Another note - I'm writing this without ANY reference to Swift documentation/information - it is a pure test to see what I know!
In the previous entry, I described how I went about learning Swift, with the timeframe that included the learning-forgetting-relearning cycle, which I am desperate to break. Last time it was the basics about variables, constants, numbers and strings. This time it gets a little more complicated...
Boolean
I believe that this comes from the (mathematician?) George Boole, who worked with absolutes: true or false. To apply this to Swift, it could be to find out whether a calculation is true or not.
E.g. 7 > 4 would return "true"
Using the <, > (along with the = for each) symbols means that you can test to see if a value is greater than another.
Conditionals
The above directly links to the use of the if/else statements. These can be very simple or much more complex...
In the above, I could change the values of number and number2 (why I put them as 'var', not 'let'), then created an if/else statement to compare the two numbers. In this case, the 'else' statement would be printed as number 2 > number.
Arrays/Dictionaries
These are similar but with a couple of notable differences: arrays are ordered whereas dictionaries are not; dictionaries use a key-value pair, which arrays do not use.
var array = ["George", "Paul", "Ringo"]
var numberArray = [1, 2, 3, 4, 5]
You can't mix up the type - a general Swift lesson and the syntax involves the square, not round, brackets. You can declare the type without any values, then use append or += to add items to the array...various things can be done.
array += ["John"]
The above is a comment - two forward slashes denote this. /* and */ are used for comments that go over one line.
To access individual items in an array, the index from 0 to... is needed. So in the array, George would be 0 etc.
Dictionaries, on the other hand, are different as they need a key-value pair:
In the previous entry, I described how I went about learning Swift, with the timeframe that included the learning-forgetting-relearning cycle, which I am desperate to break. Last time it was the basics about variables, constants, numbers and strings. This time it gets a little more complicated...
Boolean
I believe that this comes from the (mathematician?) George Boole, who worked with absolutes: true or false. To apply this to Swift, it could be to find out whether a calculation is true or not.
E.g. 7 > 4 would return "true"
Using the <, > (along with the = for each) symbols means that you can test to see if a value is greater than another.
Conditionals
The above directly links to the use of the if/else statements. These can be very simple or much more complex...
var number = 3
var number2 = 4
if number > number2 {
print("\(number) is greater than \(number2)")
} else {
print ("\(number2) is greater than \(number)")
}
*I'm going to use the font/colours from Swift by copying and pasting directly from a Playground file like the above from now on- makes it look a bit clearer!*
Arrays/Dictionaries
These are similar but with a couple of notable differences: arrays are ordered whereas dictionaries are not; dictionaries use a key-value pair, which arrays do not use.
var array = ["George", "Paul", "Ringo"]
var numberArray = [1, 2, 3, 4, 5]
You can't mix up the type - a general Swift lesson and the syntax involves the square, not round, brackets. You can declare the type without any values, then use append or += to add items to the array...various things can be done.
array += ["John"]
//The syntax is important here - again, Swift needs to know what this is: an array
To access individual items in an array, the index from 0 to... is needed. So in the array, George would be 0 etc.
var firstItemInArray = array[0]
This would return "George". I could change items in several ways, including:
array[3] = "Steve"
That would replace "John" with "Steve". Sorry, Mr Lennon!
Dictionaries, on the other hand, are different as they need a key-value pair:
var dict = [1: "Tyrion", 2: "Danaerys", 3: "Jon", 4: "Cersei"]
In the example above, there is a mix of Types, which is fine! In this case, Int-String is used. Also, the list is unordered, so that it can appear in a different order each time.
I don't know much more about these, other than some of the syntax for changing/replacing values.
dict[4] = "Jaime"
Is one way of replacing a value. That's pretty much it for dictionaries!
Loops/Control flow
These puzzle me and I know it's something that I need to work on. The essentials are OK for me for a for-in loop...
var value = 5
for i in 1...value {
print(i)
}
That would be a basic way of printing numbers 1 up to 5. My understanding is that certain conditions need to be met for the loop to happen. There are other loops: while, do-while which I'm less confident with.
There is a lot you can do with these - combining loops with else/if using && and | | to make conditions more specific...I just don't have too much understanding of this at the moment.
Next time, I'll focus on some of the more complicated aspects of Swift - enumerations, switch, functions, structures and classes...
Comments
Post a Comment