Swift (1) The Basics of Swift

Swift is a new programming language for iOS, macOS and watchOS app development.

I think Swift is a good programming language for the beginner because it is elegant and human readable.

The most important feature of Swift is that Swift introduces optional types, which handle the absence of a value. Your program becomes safe and readable because optional forces the programmer to write more explicit code (we will discuss it later).

1. Roll up your sleeves:

Let's create a new file called "hello.swift" and you can edit it with different text editors or developing tools (e.g. VSCode).

Adding our first program to "hello.swift".

print("Hello world")

Then we can execute out program in the terminal.

> swift hello.swift

Finally, you can see "Hello world" on your terminal. That's all.

The developing process is quit simple. We just edit a Swift file and execute it in the terminal.

2. Constants and variables:

Constants and variables are containers which are used to store the values. You declare constants with the let keyword and variables with the var keyword.

var name = "cooper bear"
let nickname = "ben"

The difference between constants and variables is that constants are unchangeable.

For example, you can change name:

var name = "cooper bear"
name = "cooper gold bear"

However, you can't change the constant:

let nickname = "ben"
nickname = "benson" // these will raise an error

3. Type annotations

It’s rare that you need to write type annotations because Swift can almost always infer the type to be used for that constant or variable.

Yon can explicitly describe the type of the constant or variable by writing a type annotation by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use.

var name: String = "cooper bear"
let nickname: String = "ben"

4. Optionals

Optionals are used in situations where a value may be absent.

For example, we want a user to key in his email but some user may not have an email. We can use optional variable to save the email:

var email: String?

If you define an optional variable without providing a default value, the variable is automatically set to nil which means that variable is empty

5. If Statements and forced unwrapping

An if statement is used to find out whether an optional contains a value by comparing the optional against nil.

if email != nil {
    print(email) // the email in this line is an Optional
}

Once it is sure that the optional does contain a value, we can access it by adding an exclamation mark (!) to the optional variable.

The exclamation mark tell the compiler that the variable is not optional inside the block (block is a region enclosed by the curly brackets).

if email != nil {
    print(email!) // the email in this line is a String
}

6. Optional binding

We can use optional binding to find out whether the variable is nil.

If the variable (email) is not nil the value will be assign to another temporary constant (address).

if let address = email {
    print(address)
}

留言

熱門文章