Latest web development tutorials

Swift basic grammar

In the previous section we have already talked about how to create the Swift language "Hello, World!" Program. Now we come to the next review.

If you are creating a need to introduce OS X playground Cocoa:

import Cocoa

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


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

import UIKit
var myString = "Hello, World!"

The above program, the output is:

Hello, World! 

The code above is the basic structure of the Swift program, then we described in detail part of the structure.

Swift introduced

We can use theimport statement to introduce any framework of Objective-C (or C library) to the Swift program.For exampleimport cocoa statement to import the library and use the Cocoa API, we can use them in the Swift program.

Cocoa itself is written in Objective-C language, Objective-C is a strict superset of the C language, so we can use in Swift simple mixing C language code, or even C ++ code.

Swift mark

Swift program from a variety of tokens, markers can be words, identifiers, constants, string, or symbol. Swift example, the following program consists of three tokens:



Swift's comments with C language very similar to the single-line comments begin with two backslash:


Multi-line comments begin with / * to * / end:

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

Multi-line comments and C language is different, the multi-line comment Swift can be nested within other multi-line comments inside. Wording is inserted into another multi-line comments in a multiline comment block. The second comment block when closed, then still behind the first comment block:

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

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

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

Multi-line comments nesting is that you can more quickly and easily in the comment block, even if the code block already has a comment.


Unlike other languages ​​are, Swift does not require the use of a semicolon at the end of each line of the statement (;), but you must be separated by a semicolon in the same number of lines written statement:

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


Identifier is to give variables, constants, methods, functions, enumeration, that specifies the name of the structure, class, and other protocols. Letters constituting identifiers are certain norms, naming Swift language identifiers as follows:

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

  • The first character identifiers can (_) or the letters begin with an underscore, but can not be a number;

  • Identifiers may be other characters underscore (_), letters or numbers.

For example: userName, User_Name, _sys_val, height and other legitimate identifier and 2mail, room # illegal and class identifiers.

Note: Swift letter uses Unicode encoding [1].Unicode is called the unified coding system, which contains the text encoding Asia, such as Chinese, Japanese, Korean and other characters, and even emoticons we use the chat tool

If you must use keywords as identifiers, you can add accents around the keywords ( `), for example:


A keyword is a reserved character sequence similar to an identifier, unless the use of accents ( `) to enclose, or can not be used as identifiers. Keywords are predefined special significance for the compiler reserved identifier. Common keywords following four.

And statements related keywords

class deinit enum extension
func import init internal
let operator private protocol
public static struct subscript
typealias var

And statements related keywords

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

Expression and type keyword

as dynamicType false is
nil self Self super

Keywords used in a particular 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

Swift is not a language like C / C ++, Java as completely ignoring spaces, Swift's use of the spaces have certain requirements, but unlike Python indented less stringent requirements.

In Swift, the operator can not be followed immediately at a variable or constant. For example, the following code will be given:

let a= 1 + 2

The error message is:

error: prefix/postfix '=' is reserved

Probably meant directly with the equal sign in front of or behind this usage is reserved.

The following code is being given (note the space continued):

let a = 1+ 2

The error message is:

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

This is because Swift 1+ to believe this statement was over, 2 is the next statement.

Only in this way will not write error:

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

Swift literal

The so-called literal, refers to as a specific number, string, or Boolean value this can be pointed directly to the local own type and value for the variable assignment. For example, in the following:

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