Swift Flashcards
Declare implicitly as type Int: var year = 2015
2015
unknownString = 3
3 is NSObject
unknownString = “3”
“3”
If you know that the value of a variable won’t change again, you should use let. Here, myName is declared as a constant and initialized with the value Jesse: let myName = “Jesse”
“Jesse”
Declare as optional (i.e. empty): var height: Int? friend = nil
nil nil
To assign an optional an initial value: var friend: String? = “Joe”
“Joe”
To check if there’s a vale for or optional:if let someone = friend{ print(“Hello ” + someone)}
“Hello Joe”
var b = a * 3
15
var c = b/2
2
var c: Double = 5/2
2.5
c * 2
5
c * [integer 4]
10
c * Double(int)
10
Tuples are combined values that always appear together. Let person = (“Joe”, 24) print(person.0) print(person.1)
Joe 24
Often we want to refer to members in a tuple by name rather than index value (e.g. .0, .1, .2, etc.). let (name, age) = person print(name)
Joe