标识符
- ASCII字符和Unicode字符
- 不允许包含whitespace字符,数学符号,箭头,非法Unicode点,线(-),box-drawing字符
- 不允许以数字开头
- 如果标识符和关键字重名,用‘ ’包括
- 大小写敏感
注:标识符规则限制目的为了避免二义性,标识符不允许以数字开头是因为编译器无法识别123(十进制),0123(八进制),0xabc(十六进制)等属于标识符还是整型字面值,不允许包含线(-)是因为编译器无法识别a-b等属于标识符还是减法表达式,其他等等
func identifier()
{
let food = 5
let 食物 = 8
let π = 3.14
let `let` = 58
print("food = \(food), 食物 = \(食物), π = \(π), `let` = \(`let`)")
}
output:
food = 5, 食物 = 8, π = 3.14, `let` = 58
注释
- 支持单行注释
- 支持多行注释
- 支持多行注释嵌套
func commet()
{
// This is a comment.
/* This is also a comment
but is written over multiple lines. */
/* This is the start of the first multiline comment.
/* This is the second, nested multiline comment. */
This is the end of the first multiline comment. */
}
注:支持多行注释嵌套能简单快速注释掉代码块,特别是代码块自身已包含多行注释
语句格式
分号(;)不是语句组成部分,分号(;)只是语句分隔符
func sentence()
{
var food = 0
food = 1;
food = 2
food = 3; food = 4
}
注:换行自然是语句分隔符