Swift Foundational Addons I: Variables and Constants

Swift Foundational Addons I: Variables and Constants

 次点击
14 分钟阅读

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 structs, unlike some old-world languages, e.g. C language. Int is to Swift what Integer Java is to Swift, as 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 a unique expression that you can find in Swift. There is a sample code:

if let constantName = someOptional {
    statements1
} else {
    statements2
}

if-let code block means: if someOptional do not have a value, then execute statment2; if someOptional has an 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 execute only if ojb1 and obj2 have their values and tmp1 must be smaller than tmp2. If any of these conditions 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 ensure 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 convert them before putting 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

© 本文著作权归作者所有,未经许可不得转载使用。