If you know that you are not going to or not suppose to change the value of a variable , it’s best to declare it as constant so when you try to change it, Xcode will throw error. In a way, it’s a safe guard.
=======================
User can choose to explicitly define the type annotation or let swift decide it, also known as type inference.
let score = 20.0
is the same as
let score : Double
score = 20.0
Float vs Double
As for which one to use, it depends on how big the number you want to store
var longitude: Float
longitude = -86.783333 //78333
longitude = -186.783333 //7833
longitude = -1286.783333 //783
longitude = -12386.783333 //78
longitude = -123486.783333 //8
longitude = -1234586.783333 //no more faction
Since Float has limited space, it prioritise round number first, then only do the fraction number. If you change it to Double, then no issue at all
Boolean - probably gonna skip this one since it’s pretty straight forward
Those are the basic operators, then there’s the -=, +=.
Then follow by “Comparison Operators”
> , < , >= , <= , == , !=
“This is a fancy name for what is actually a very simple thing: combining variables and constants inside a string.”
I like this definition by the author, haha. “Fancy Name”
var name = "Tim McGraw"
var age = 25
var latitude = 36.166667
"Your name is \(name), your age is \(age), and your latitude is \(latitude)"
The “\()” is the key. For sure we can do it with “+” as well, but that’s only applicable when you try combine with string only. If string + Int or other data type, it will not work.
var evenNumbers = [2, 4, 6, 8]
var songs = ["Shake it Off", "You Belong with Me", "Back to December"]
With type inference, swift know that evenNumbers is an array of Int, and songs is an array of String
To know the actual type of the array, you can print type(of: songs) , then you will see the data type.
var songs = ["Shake it Off", "You Belong with Me", "Back to December", 3]
Swift will throw error since type inference can’t handle mixed data type, and to explicitly define it, you can use [Any] , means it accept any kind of data. But this is not a good practice as it will involve a lot of unwrapping to do to confirm the data type before assign to a variable or constant
And how to create an empty array? There are 2 choices
var songs: [String] = []
var songs = [String]()
And there’s operator for array as well
var songs = ["Shake it Off", "You Belong with Me", "Love Story"]
var songs2 = ["Today was a Fairytale", "Welcome to New York", "Fifteen"]
var both = songs + songs2
both += ["Everything has Changed"]
+= will work, but -= won’t work in this case so must take note.
=========================
var person = ["Taylor", "Alison", "Swift", "December", "taylorswift.com"]
If we store a person info in an array, we won’t know how to get the data easily as all the data go by index.
var person = ["first": "Taylor", "middle": "Alison", "last": "Swift", "month": "December", "website": "taylorswift.com"]
If we change to dictionary, value can be retrieve by key, such as person[“first”]. Both array and dictionaries also required an identifier to obtain the value BUT, one of them is index, the other one is key, for the key, we can label it with something more meaningful and easier to remember, compare to index, which is 0,1,2,3,4,5
=========================
Basically is “if”, “else” , “if else”.
And a few operators, “==” , “||” , “&&” , “!” (NOT)
==========================
Standard For loops, with … , ..< and _ (underscore) when value not needed
for i in 1...10 {
print("\(i) x 10 is \(i * 10)")
}
for i in 1..<10 {
print("\(i) x 10 is \(i * 10)")
}
for _ in 1 ... 5 {
str += " fake"
}
Sample for looping an array
var songs = ["Shake it Off", "You Belong with Me", "Look What You Made Me Do"]
for song in songs {
print("My favorite song is \(song)")
}
For loop in a for loop
var people = ["players", "haters", "heart-breakers", "fakers"]
var actions = ["play", "hate", "break", "fake"]
for i in 0 ..< people.count {
var str = "\(people[i]) gonna"
for _ in 1 ... 5 {
str += " \(actions[i])"
}
Then there’s the “while” , “do while” , and “break” , along with for loop labelling to break a few nested loop at the same time.
switch liveAlbums {
case 0:
print("You're just starting out")
case 1:
print("You just released iTunes Live From SoHo")
case 2:
print("You just released Speak Now World Tour")
default:
print("Have you done something new?")
}
Nothing much to explain for this. Straight forward