Swift basic syntax

In the previous section we talked about how to create the "Hello, World!" program in swift language. Now let's review it.

If you're creating an OS X playground, you need to introduce Cocoa:

import Cocoa

/* 我的第一个 Swift 程序 */
var myString = "Hello, World!"

print(myString)

If we want to create iOS playground, we need to introduce UIKit:

import UIKit
var myString = "Hello, World!"
print(myString)

The above procedure is performed and the output is:

Hello, World! 

The above code is the basic structure of the Swift program, and then let's elaborate on the components of the structure.


Swift introduced

We can use the import statement to introduce any Objective-C framework (or C library) into the Swift program. For example, the import cocoa statement imports using the Cocoa library and APIs, which we can use in Swift programs.

Cocoa itself is written in the Objective-C language, which is a strict hyperset of the C language, so in Swift applications we can simply mix C-language code, even C-code.


Swift tag

The Swift program consists of a variety of tags that can be words, identifiers, constants, strings, or symbols. For example, the following Swift program consists of three types of tags:

print("test!")
标记是:单词、符号
print
(
   "test!"
)

Comments

Swift's comments are very similar to the C language, with a single line of comments beginning with two backslashes:

//这是一行注释

Multi-line comments start with /) and end with /:

/* 这也是一条注释,
但跨越多行 */

Unlike multi-line comments in the C language, Swift's multi-line comments can be nested within other multi-line comments. T he way to write is to insert another multi-line comment within a multi-line comment block. W hen the second comment block is closed, the first comment block is followed by:

/* 这是第一个多行注释的开头

/* 这是嵌套的第二个多行注释 */

这是第一个多行注释的结尾 */

The nesting of multi-line comments is a comment block that you can make faster and easier, even if you already have comments in the block.


Semicolon

Unlike other languages, Swift does not require a sign (;)) at the end of each line of statement, but when you write multiple statements on the same line, you must separate it:

import Cocoa
/* 我的第一个 Swift 程序 */
var myString = "Hello, World!"; print(myString)

Identifier

Identifiers are the names given to variables, constants, methods, functions, enumerations, structures, classes, protocols, and so on. The letters that make up the identifier have certain specifications, and the naming rules for identifiers in the Swift language are as follows:

  • Case sensitive, Myname and myname are two different identifiers;

  • The identifier's first character can start with the _ dash ( , ) or the letter, but not the number;

  • The other characters in the identifier can be underscores, _ letters, or numbers.

For example: userName, User_Name, _sys_val, height, etc. are legitimate identifiers, while 2mail, room, and class are illegal identifiers.

Note: T he letters in Swift are coded by Unicode. Unicode, called unified coding, contains Asian text coding, such as characters such as Chinese, Japanese, Korean, and even the emojis we use in chat tools

If you must use keywords as identifiers, you can add accents (') before and after keywords, such as:


Keywords

A keyword is a reserved sequence of characters similar to an identifier and cannot be used as an identifier unless it is enclosed with accents ('). K eywords are predefined retention identifiers that have special meaning for the compiler. There are four common keywords.

Keywords related to the claim

class deinit Enum extension
func import Init internal
let operator private protocol
public static struct subscript
typealias Var

Keywords related to statements

break case continue default
do else fallthrough for
if in return switch
where while

Expression and type keywords

as dynamicType false is
nil self Self super
true _COLUMN_ _FILE_ _FUNCTION_
_LINE_

Keywords used in a specific context

associativity convenience dynamic didSet
final get infix inout
lazy left mutating none
nonmutating optional override postfix
precedence prefix Protocol required
right set Type unowned
weak willSet

Swift space

The Swift language doesn't completely ignore spaces like C/C, Java, and Swift has some requirements for the use of spaces, but it's not as strict as Python's requirements for indentation.

In Swift, operators cannot be directly followed by variables or constants. For example, the following code will report an error:

let a= 1 + 2

The error message is:

error: prefix/postfix '=' is reserved

This means that the use of equal marks directly in front or behind is reserved.

The following code still reports errors (continue to pay attention to spaces):

let a = 1+ 2

The error message is:

error: consecutive statements on a line must be separated by ';'

This is because Swift thinks that the statement is over by 1 plus, and 2 is the next statement.

Only by writing like this will you not report an error:

let a = 1 + 2;  // 编码规范推荐使用这种写法
let b = 3+4 // 这样也是OK的

Swift literally

Literally, it refers to values like a specific number, string, or Boolean value that can directly point out your own type and assign values to variables. For example, here's what:

42                 // 整型字面量
3.14159            // 浮点型字面量
"Hello, world!"    // 字符串型字面量
true               // 布尔型字面量