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 ImportWe can use theimport
statement to introduce any Objective-C framework (or C library) into a Swift program. For example, the
import cocoastatement 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.import UIKit var myString = "Hello, World!" print(myString)CommentsSwift 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.
SemicolonUnlike 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:/* 这也是一条注释, 但跨越多行 */IdentifierIdentifier 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.
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 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
Keywords related to declaration
##Keywords related to statementsclass deinit enum extension func import init internal let operator private protocol public static struct subscript typealias var
Expression and type keywordsbreak case continue default do else fallthrough for if in return switch where while ##as
nildynamicType false is trueself Self super _LINE__COLUMN_ _FILE_ _FUNCTION_ Keywords used in a specific contextassociativity
finalconvenience dynamic didSet lazyget infix inout nonmutatingleft mutating none precedenceoptional override postfix right prefix Protocol required weakset Type unowned ##Swift SpaceswillSet
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: /* 这是第一个多行注释的开头
/* 这是嵌套的第二个多行注释 */
这是第一个多行注释的结尾 */
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 + 2The error message is:
error: prefix/postfix '=' is reservedThis 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: