当前位置: 首页 > 文档资料 > Swift 中文教程 >

Basic 语法

优质
小牛编辑
129浏览
2023-12-01

我们在设置环境时已经看过一段Swift 4程序。 让我们再次开始使用以下Hello, World! 为OS X playground创建的程序,包括import Cocoa ,如下所示 -

/* My first program in Swift 4 */
var myString = "Hello, World!"
print(myString)

如果你为iOS游乐场创建相同的程序,那么它将包含import UIKit ,程序将如下所示 -

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

当我们使用适当的游乐场运行上述程序时,我们将得到以下结果 -

Hello, World!

现在让我们看一下Swift 4程序的基本结构,这样您就可以轻松理解Swift 4编程语言的基本构建块。

在Swift 4中导入

您可以使用import语句将任何Objective-C框架(或C库)直接导入到Swift 4程序中。 例如,上面的import cocoa语句使得构成所有OS X的开发层的所有Cocoa库,API和运行时都可以在Swift 4中使用。

Cocoa是在Objective-C中实现的,它是C的超集,因此很容易将C甚至C ++混合到Swift 4应用程序中。

Swift 4中的标记

Swift 4程序由各种令牌组成,令牌可以是关键字,标识符,常量,字符串文字或符号。 例如,以下Swift 4语句由三个标记组成 -

print("test!")
The individual tokens are:
print("test!")

注释 (Comments)

评论就像在Swift 4程序中帮助文本一样。 它们被编译器忽略。 多行注释以/ *开头,并以字符* /结尾,如下所示 -

/* My first program in Swift 4 */

多行注释可以嵌套在Swift 4中。以下是Swift 4中的有效注释 -

/* My first program in Swift 4 is Hello, World!
/* Where as second program is Hello, Swift 4! */ */

在注释的开头使用//编写单行注释。

// My first program in Swift 4

分号(Semicolons)

Swift 4不要求你在代码中的每个语句后键入分号(;),尽管它是可选的; 如果你使用分号,那么编译器不会抱怨它。

但是,如果在同一行中使用多个语句,则需要使用分号作为分隔符,否则编译器将引发语法错误。 你可以写上面的Hello,World! 计划如下 -

/* My first program in Swift 4 */
var myString = "Hello, World!"; print(myString)

标识符 (Identifiers)

Swift 4标识符是用于标识变量,函数或任何其他用户定义项的名称。 标识符以字母A到Z或a到z或下划线_开头,后跟零个或多个字母,下划线和数字(0到9)。

Swift 4不允许在标识符中包含特殊字符,如@,$和%。 Swift 4是一种case sensitive编程语言。 因此,人力和人力是Swift 4中的两个不同标识符。以下是可接受标识符的一些示例 -

Azad        zara   abc   move_name   a_123
myname50    _temp  j     a23b9       retVal

要使用保留字作为标识符,您需要在它之前和之后放置一个反引号(`)。 例如, class不是有效的标识符,但`class`是有效的。

关键字 (Keywords)

Swift 4中保留了以下关键字。这些保留字不能用作常量或变量或任何其他标识符名称,除非它们用反引号转义 -

声明中使用的关键字

ClassdeinitEnumextension
FuncimportInitinternal
Letoperatorprivateprotocol
publicstaticstructsubscript
typealiasvar

报表中使用的关键字

breakcasecontinuedefault
doelsefallthroughfor
ifinreturnswitch
wherewhile

表达式和类型中使用的关键字

asdynamicTypefalseis
nilselfSelfsuper
true_COLUMN__FILE__FUNCTION_
_LINE_

在特定情境中使用的关键字

associativityconveniencedynamicdidSet
finalgetinfixinout
lazyleftmutatingnone
nonmutatingoptionaloverridepostfix
precedenceprefixProtocolrequired
rightsetTypeunowned
weakwillSet

Whitespaces

只包含空格(可能带有注释)的行称为空行,Swift 4编译器完全忽略它。

Whitespace是Swift 4中用来描述空格,制表符,换行符和注释的术语。 空格将语句的一部分与另一部分分开,并使编译器能够识别语句中的一个元素(如int)的结束位置以及下一个元素的开始位置。 因此,在以下声明中 -

var age

varage之间必须至少有一个空格字符(通常是空格),以便编译器能够区分它们。 另一方面,在以下声明中 -

int fruit = apples + oranges   //get the total fruits

水果和=之间,或者=和苹果之间不需要空格字符,尽管您可以自由地包含一些以提高可读性。

运算符两侧的空间应相等,例如。

int fruit = apples +oranges    //is a wrong statement
int fruit = apples + oranges   //is a Correct statement

Literals

文字是整数,浮点数或字符串类型的值的源代码表示。 以下是文字的例子 -

92               // Integer literal
4.24159          // Floating-point literal
"Hello, World!"  // String literal

在Swift中打印

要在swift中打印任何内容,我们都有'print'关键字。

打印有三种不同的属性。

Items - 要打印的项目

Separator - 项目之间的Separator

Terminator - 行应该结束的值,让我们看一个例子和语法。

print("Items to print", separator: "Value " , terminator: "Value")
// E.g. of print statement.
print("Value one")
// prints "Value one \n" Adds, \n as terminator and " " as separator by
default.
print("Value one","Value two", separator: " Next Value" , terminator: " End")
//prints "Value one Next Value Value two End"

在上面的代码中,第一个print语句默认添加\ n,newline Feed作为终结符,而在第二个print语句中我们给出了“End”作为终结符,因此它将打印“End”而不是\ n。

我们可以根据我们的要求提供我们的定制分离器和终结器。