Swift 語法
注釋用于使程序更加容易理解。愛掏網 - it200.com它們就像程序中的幫助文本一樣,并且編譯器會忽略它們。愛掏網 - it200.com在Swift 4中,單行注釋是使用 // 開頭的注釋。愛掏網 - it200.com
在Swift 4中的單行注釋
// This is a single line comment.
多行注釋在Swift 4中的使用
多行注釋以/開始,以/結束,如下所示-
/* This is multiline comment */
Swift 4支持嵌套的多行注釋。愛掏網 - it200.com即
/* This is a multi-line comment.
/* This is the second line. */ */
在Swift中的分號
在Swift 4中,您不需要在代碼中輸入分號(;)作為結束語句。愛掏網 - it200.com盡管它是可選的,但您可以在不出問題的情況下使用它。愛掏網 - it200.com如果您在同一行中使用多個語句,則必須使用分號作為分隔符,否則編譯器將出現語法錯誤。愛掏網 - it200.com
示例
/* First Swift 4 program */
var myString = "Hello, World!"; print(myString)
不使用分號的方法
/* First Swift 4 program */
var myString = "Hello, World!"
print(myString)
Swift中的標識符
在Swift 4中,標識符用于標識變量、函數或其他用戶定義的項。愛掏網 - it200.comSwift 4的標識符以字母A到Z或a到z或下劃線_開頭,后面可以是零個或多個字母、下劃線和數字(0到9)。愛掏網 - it200.com
在Swift 4中,我們不能在標識符中使用特殊字符,如@
、$
和%
。愛掏網 - it200.comSwift 4是大小寫敏感的編程語言,因此 Literal 和 literal 是兩個不同的標識符。愛掏網 - it200.com
這些是一些可接受的標識符示例:
Ajeet sonoo ak_47
如果你想使用保留字作為標識符,你需要在該保留字的前后加上反引號(`)。愛掏網 - it200.com例如,class不是一個有效的標識符,但是class
是有效的。愛掏網 - it200.com
Swift中的保留關鍵字
在Swift 4中,保留關鍵字不能用作常量、變量或任何其他標識符名稱。愛掏網 - it200.com如果你想將它們用作標識符,你需要在它們周圍使用反引號('
)。愛掏網 - it200.com
用于聲明的關鍵字
Class | Func | Let | public |
---|---|---|---|
deinit | Enum | extension | import |
Init | internal | operator | private |
protocol | static | struct | subscript |
typealias | var |
語句中使用的關鍵詞
break | case | continue | default |
---|---|---|---|
do | else | fallthrough | for |
if | in | return | switch |
where | while |
在表達式和類型中使用的關鍵字
as | dynamicType | false | is |
---|---|---|---|
nil | self | Self | super |
true | _COLUMN_ |
_FILE_ |
_FUNCTION_ |
_LINE_ |
用于特定語境中的關鍵詞
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中的空格
在Swift 4中,空格用于描述空白、制表符、換行符和注釋。愛掏網 - it200.com它將語句的一部分與另一部分分隔開來。愛掏網 - it200.com它使計算機能夠識別出一個元素的結束和另一個元素的開始。愛掏網 - it200.com
示例
var age
我們必須在var和age之間加上至少一個空白字符(通常是一個空格),以使編譯器能區分它們。愛掏網 - it200.com
另一方面,在以下語句中 –
int courses = html + css //discount on the combined course
不需要在課程和=之間或者=和html之間加入空格字符,盡管你可以為了更好的可讀性而包括它們。愛掏網 - it200.com
你應該在運算符的兩側留出同樣的空間。愛掏網 - it200.com
示例
int courses = html + css //Correct statement
int courses= html+ css //Incorrect statement
Swift 4編譯器忽略只包含空格的空行。愛掏網 - it200.com
Swift中的字面值
字面值用于表示整數、浮點數或字符串類型的值的源代碼。愛掏網 - it200.com
示例
整數字面值
26
浮點字面常量
3.14159
String literal
"Hello, JavaTpoint!"
打印語句在Swift中的使用
在Swift4中,我們可以使用print
關鍵字來打印信息。愛掏網 - it200.comprint
關鍵字有三個不同的屬性。愛掏網 - it200.com
- Items(項目): 你想要打印的項目。愛掏網 - it200.com
- Separator(分隔符): 用于分隔項目。愛掏網 - it200.com
- Terminator(結尾符): 用于指定行的最后值。愛掏網 - it200.com
示例
print("Items you want to print", separator: "Value " , terminator: "Value")
// 打印語句的例子
print("Value one")
// 默認情況下,打印"Value one \n",換行符作為結尾符,空格作為分隔符。愛掏網 - it200.com
print("Value one","Value two", separator: " Next Value" , terminator: " End")
// 打印"Value one Next Value Value two End"
第一個打印語句默認添加了換行符\n
作為結尾符,而第二個打印語句中我們指定了” End “作為結尾符,所以它會打印”End “而不是\n
。愛掏網 - it200.com
我們可以根據需求使用自定義的分隔符和結尾符。愛掏網 - it200.com