Intro to Swift Flashcards
What is swift?
- An object oriented language which employs modern language theory concepts and exploits a number of object oriented principles.
- It simplifies objective C
What is the standard naming scheme for naming constants and variables in swift?
- start with lowercase letter and use camel case
- types can be inferred from default values or else you’ll need to specify them
- no ; needed at the end of lines
When do we use let and var when first defining/assigning values?
- we use let for constants
- we use var for variables
What does swift let you put in large integers to improve readability?
- Lets you include _
- for example = var speed = 186_000_000
- If you print these, the _ doesn’t appear
What are standard types in swift?
String, Double, Int, Bool, non standard types (such a tuples)
How to generate numbers in swift?
Int.random(in: 1…6) // gives a value between 1 and 6
How do you assign values to a tuple?
let httpError: (code: Int, message: String) = (404, “Not Found”)
print (httpError.message)
How do we refer to components of a tuple numerically?
print (httpError.1)
print (httpError.0)
In C and objective C what are strings?
strings are arrays in ASCII characters
What are strings composed of in swift?
Strings are composed of unicode characters and each character may be represented by a multi-byte sequence
What type are strings?
Value types
What is an extended grapheme cluster?
An extended grapheme cluster is a sequence of one or more Unicode scalars that (when combined) produce a single human-readable character
How do you do multiline strings in swift?
- use 3 quotation marks
- ””” bla bla
bla bbla
bal “””
What do you use to prevent different lines inside a multiline string?
a \
what can the # symbol be used for with strings in swift
- for treating characters literally: #”Did you say “help me”?”# prints
Did you say “help me”? - for inserting variables into a string: let user = “Phil”, let myString = #”Did you say “help me” (user) ?”#
How do we index a string?
- using a string.Index or a range
- can create an index toaccess a specific character based on those indexes?
How can we access every single individual characters of a string
by iterating over the string itself
What function can we use to determine the length of a string?
.count
What does swifts use of extended grapheme clusters for character values mean?
That concatenating two strings may not increase the string’s character count
What type are optionals in swift?
Non-standard types
When are optionals used in swift?
They are used where a value may be absent e.g. trying to convert a string to an integer.
What happens when we define something as an optional variable?
It automatically initialises it’s value to nil
How is nil different to an empty string?
Empty value doesn’t mean “” (an empty string) it’s simply nothing, which we can’t return, that’s why we have to initialise it to NIL.
How do we specify that something has an optional type?
we use a ?
- so we could say:
var survey: String?
how do we check if a variable with optional type has a value?
- Can use an “if” statement
If an variable with an optional types does have a value after checking if it does, how do we get that data?
- By If it does we can unwrap it using a ! This is known as force unwrapping
Why must we check if a variable has a value of NIL before force unwrapping it?
- If we unwrap a NIL value, the program will crash.
What is optional chaining?
- put a ? instead of ! during the force unwrap
- it will attempt to unwrap a NIL value but the program won’t crash
How can ?? be used to carry out different outcomes?
If the thing on the left of the ?? evaluates to NIL, it returns the thing on the right of ??
What do you have to do when you create a variable?
When you create a variable it has to be initialised or made an optional variable so that it has a default value.
What are the collection types in swift?
Arrays, Sets, Dictionaries
What are the differences between sets, arrays and dictionaries
- arrays are ordered collections of values
- sets are unordered collections of unique values
- dictionaries are unordered collections of key-value pairs
How do you define an array?
- user square brackets
- let a = [1,2,1,5,2,7,8,4]
How do you define sets?
- have to use set keyword
- var b: Set<String> = ["blues","jazz","soul"]</String>
How do you define a dictionary?
- colons between key and value
- var c = [2: “blue”, 3:”bluer”, 4:”bluest”]
What is true about arrays and types?
Once an array has a type it cannot be changed
What does it mean that an array is dynamic?
The size and values of the array can change
How do we use different types in an array where each object must have the same type?
- We declare the array as having type
- as [any]
What number does indexing start at in arrays?
- indexing starts at 0
How can we get the size of an array?
- By using the .count function on it
How can we return the 3rd element of an array?
- by doing the arrayName[2]
- because indexing starts at 0
What keyword do we use to add an item to an array?
arrayName.append(newValue)
How do we update the value of an item in an array?
- We access it using indexing and assign it to the new value
- for example arrayName[0] = 4
How do we iterate through every item in an array?
- for i in arrayName {
print(arrayName[i])
}
What does the .sorted, .sort and .shuffle and .shuffled functions do?
- .Sorted returns a sorted array, where as .sort modifies the actual array by sorting it
- .Shuffled returns a shuffled array, where as .shuffle modifies the actual array by shuffling it
Is membership testing faster in arrays or sets?
It’s faster in sets (because items must be unique)
When we create a set what must we define?
- if it’s empty upon initialisation