Defining
We can declare multiple constants or multiple variables on a single line, separated by commas.
var a = 1, b = 2.9, c = "string" //euals to var a: Int = 1, b: Double = 2.9, c: String = "string"
However, there is a fact that we may define multiple related variables of the same type on a single line, we shall add a single type annotation after the final variable name.
//three Int are being declared var a, b, c: Int
Basic Types
In swift, basic types are actually struct, unlike some old-world language, e.g. C lang. Int
is to Swift what Integer
is to Java, hereby it provides us a lot of built-in functions and its boundaries.
//print boundaries print(UInt32.max)
Tuples group multiple values into a single compound value. The values within a tuple can be of any type and don’t have to be of the same type as each other.
let http404Error = (404, "Not Found")
We can decompose a tuple’s contents into separate constants or variables, which we then access as usual:
let (statusCode, statusMessage) = http404Error print("The status code is \(statusCode)") // Prints "The status code is 404" print("The status message is \(statusMessage)") // Prints "The status message is Not Found"
If we only need some of the tuple’s values, ignore parts of the tuple with an underscore (_
) when we decompose the tuple:
let (justTheStatusCode, _) = http404Error print("The status code is \(justTheStatusCode)") // Prints "The status code is 404"
Optional
if-let
is an unique experssion that you can find in swift. There is an sample code:
if let constantName = someOptional { statements1 } else { statements2 }
if-let
code block meas: if someOptional
do not have a value, then execute statment2
; if someOptional
has a actual value, then define a new constant constantNAme
with the same value of someOptional
, then execute statement1
.
We can combine some extra code within if-let
expression to simplify code.
var obj1: Int? = 1 var obj2: Int? = 2 if let tmp1 = obj1, tmp2 = obj2, tmp1 < tmp2 { statements1 } else { statements2 }
In this case, statements executes only if ojb1
and obj2
have their values and tmp1
must be smaller that tmp2
. If any of these condistions don’t meet if-let
expectations, only statements2
will be executed.
An implicitly unwrapped optional is a normal optional behind the scenes, but can also be used like a non-optional value, without the need to unwrap the optional value each time it’s accessed.
var obj3: Int! obj3 = 1 print(obj3)
However, please note that we must assure an implicitly unwrapped optional has its value assigned before we access it without explicit unwrapping.
Operation
Please note that there is no implicit type conversion in Swift, we have to covert them before puting them together, for example, we cannot simply add 1
with 1.0
. We need to explicitly convert 1: Int
to 1: Double
before it can be added with 1.0
.
var a: Int = 1 var b: Double = 1.0 let res1 = a + b //wrong let res2 = Double(a) + b //bingo
Greetings! Very helpful advice in this particular post! It’s the little changes that produce the largest changes. Many thanks for sharing!
Thanks for your agreement! I’m still a college student learning basic skills of Swift, and I will post more articles of some obstacles I may meet in the future.😃