Swift basic syntax


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

If we are creating an OS

import Cocoa

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

print(myString)

The above code is the basic structure of the Swift program. Next, we will explain the components of the structure in detail.

Swift Import

We can use the
import

statement to introduce any Objective-C framework (or C library) into a Swift program. For example, the

import cocoa

statement imports the Cocoa library and API, and we can use them in Swift programs.

Cocoa itself is written in Objective-C language. Objective-C is a strict superset of C language, so we can easily mix C language code into Swift applications. , even C++ code.


Swift Tags

Swift programs are composed of a variety of tags, which can be words, identifiers, constants, strings, or symbols. For example, the following Swift program consists of three tags:
import UIKit
var myString = "Hello, World!"
print(myString)

Comments

Swift comments are very similar to C language. Single-line comments start with two backslashes:

Hello, World!

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

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

Different from multi-line comments in C language, Swift’s multi-line comments Can be nested inside other multi-line comments. The way to write it is to insert another multi-line comment inside a multi-line comment block. When the second comment block is closed, it is still followed by the first comment block:

//这是一行注释

Nesting of multi-line comments allows you to comment code blocks more quickly and conveniently, even if there are already comments in the code block.

Semicolon

Unlike other languages, Swift does not require a semicolon (;) at the end of each line of statements, but when you write multiple statements on the same line must be separated by semicolons:
/* 这也是一条注释,
但跨越多行 */

Identifier

Identifier is specified for variables, constants, methods, functions, enumerations, structures, classes, protocols, etc. name. The letters that make up identifiers have certain specifications. The naming rules for identifiers in Swift language are as follows:

is case-sensitive, Myname and myname are two different identifiers;

  • The first character of the identifier can start with an underscore (_) or a letter, but cannot be a number;

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

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

Note:

The letters in Swift use Unicode encoding [1]. Unicode is called the unified encoding system. It includes Asian character encodings, such as Chinese, Japanese, Korean and other characters, and even the emoticons we use in chat tools

If you must use keywords as identifiers, you can Add accent marks (`) before and after keywords, for example:


Keywords

Keywords are identifier-like sequences of reserved characters that cannot be used as identifiers unless they are enclosed in accents (`). Keywords are predefined reserved identifiers that have special meaning to the compiler. Common keywords include the following 4 types.

Keywords related to declaration

classdeinitenumextension
funcimportinitinternal
letoperatorprivateprotocol
publicstaticstruct subscript
typealiasvar

##Keywords related to statements

breakcasecontinuedefaultdoelsefallthroughforif inreturnswitchwherewhile


Expression and type keywords

##asniltrue_LINE_Keywords used in a specific context
dynamicType falseis
selfSelfsuper
_COLUMN__FILE__FUNCTION_



associativityfinallazynonmutatingprecedencerightweak##Swift Spaces
conveniencedynamicdidSet
getinfixinout
leftmutatingnone
optionaloverridepostfix
prefixProtocolrequired
setTypeunowned
willSet


The Swift language does not completely ignore spaces like C/C++ and Java. Swift has certain requirements for the use of spaces, but it is not as strict as Python’s requirements for indentation. .

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

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

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

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

The error message is:

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

It means that the usage of the equal sign directly preceding or following it is reserved.

The following code will still report an error (continue to pay attention to the spaces):

let a= 1 + 2

The error message is:

error: prefix/postfix '=' is reserved

This is because Swift thinks that the statement ends at 1+. 2 is the next statement.

Only writing like this will not report an error:

let a = 1+ 2

Swift literal


The so-called literal refers to a specific number, string or Boolean value In this way, you can directly point out your own type and assign a value to the variable. For example, below:

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