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?