SWIFT Basics Flashcards
What is an array?
Array is an ordered list e.g. var hans : [String] = [“Peter”,”Tina”,”Julia”]
What is a Dictionary
Dictionary is an unordered list var name =[“Hans”:”Bla”,”Julia”:”Bla2”] key:value
How do I access item of Dictionary?
nameoflist [“Key”]
How do I add item later to Dictionary
nameoflist [“NewKey”] = “New Value”
For - In Loop When used? How does it work? How does a range work? What is a closed range? What is the alternative?
used after array
for item in nameofvar { println(item) } // Each value in array will be assigned individually to constant here named item. Than when exectuing command printing item, it repeats print function until reaching end of array For a range use: for number in 1…10 { println(“(number) times 2 is (number*2)” } //This includes 10: for until 9 use 1..10
What is a While loop? How does it look like? How does the Do-While loop differ? What do I have to do that loop is not repeating over and over?
Used after array e.g. while onTrain { readBook()} // Code checks onTrain for True/False. If true, it keeps performing { } until false need to create after array a var index =0 to compare for true/false (while loop has index) than: while index < todo.count { println(todo[index]) } //To prevent loop repetation, need to increment index before final } incrementing write index++ Do-While loop ensures that true/ falls condition is executed last, instead of first. Used in cases where loop should be at least executed once e.g. do { } while index < todo.count
What is a unary operator? When to use before and when after? Is it differently used in While loop?
++ or – // before or after word Used before ++Name for new value, used after Name++ to use with previous values Used different loop, if after it will first assign the value and than increment the var: if prefixed it will first increment the var and than assign
What is For-Condition-Increment? How is it used?
For-Condition-Increment is used like while loop but shorter e.g. for var index = 0; index < todo.count; index++ {println(todo[index]) }
What are comparative Operators?
< less than greater than >= greater than or equal == equal != not equal === identical !== not identical
Control can be divided into??
Two types - loops and conditionals
What is an if statement?
If on train read book
else if in car listen to music
else play game
Can create chains with else if
What is a Switch Statement?
- it is similar like if statement (case function replaces else)
Code e.g.
let cards = 1…13
for card in cards {
switch card {
case 11:
println(“Jack”)
case 12:
println(“Queen”)
default:
println(card)
} // default case needs to be used if not all defined
With switch can use power functions. e.g. define a range
case 1,11…13:
println(“Bla”)
What is an operator and where are they used?
- Used in if statements
- there are two types
- Comparison Operator
- 1==1 ; 1!=1 ; 1 > 1, 1 >= 1 etc.
- Logical Operator (3 types)
- && AND operator (both expression have to be true)
- || OR operator (one of two expression needs to be true)
- ! Not operator
- e.g. code if distance > 5 && distance > 20 {
println(“AND”)}
- Comparison Operator