The following keywords are reserved and can’t be used as identifiers, unless they’re escaped with backticks, as described above in Identifiers. Keywords other than inout, var, and let can be used as external parameter names in a function declaration or function call without being escaped with backpacks.
Keywords used in declarations:
associatedtype, class,
deinit :對象銷毀的調(diào)用,多用于資源釋放帆竹,數(shù)據(jù)庫的關(guān)閉等等
enum, extension,
func, import, init,
inout:輸入輸出參數(shù)(In-Out Parameters)
變量參數(shù)殖熟,僅僅能在函數(shù)體內(nèi)被更改角塑。如果你想要一個函數(shù)可以修改參數(shù)的值蟹漓,并且想要在這些修改在函數(shù)調(diào)用結(jié)束后仍然存在栏赴,那么就應(yīng)該把這個參數(shù)定義為輸入輸出參數(shù)(In-Out Parameters)镊辕。
定義方法是在參數(shù)定義前加 inout 關(guān)鍵字痛阻。一個輸入輸出參數(shù)有傳入函數(shù)的值菌瘪,這個值被函數(shù)修改,然后被傳出函數(shù)阱当,替換原來的值俏扩。
你只能傳入一個變量作為輸入輸出參數(shù)。你不能傳入常量或者字面量(literal value)弊添,因為這些量是不能被修改的录淡。當(dāng)傳入的參數(shù)作為輸入輸出參數(shù)時,需要在參數(shù)前加&符油坝,表示這個值可以被函數(shù)修改嫉戚。
注意: 輸入輸出參數(shù)不能有默認(rèn)值,而且可變參數(shù)不能用 inout 標(biāo)記澈圈。如果你用 inout 標(biāo)記一個參數(shù)彬檀,這個參數(shù)不能被 var 或者 let 標(biāo)記。
下面是例子瞬女,swapTwoInts 函數(shù)窍帝,有兩個分別叫做 a 和 b 的輸入輸出參數(shù):
func swapTwoInts(inout a: Int, inout b: Int) {
let temporaryA = a
a = b
b = temporaryA
}
這個 swapTwoInts 函數(shù)僅僅交換 a 與 b 的值。該函數(shù)先將 a 的值存到一個暫時常量 temporaryA 中拆魏,然后將 b 的值賦給 a盯桦,最后將 temporaryA 幅值給 b。
你可以用兩個 Int 型的變量來調(diào)用 swapTwoInts渤刃。需要注意的是,someInt 和 anotherInt 在傳入 swapTwoInts 函數(shù)前贴膘,都加了 & 的前綴:
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
println("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// prints "someInt is now 107, and anotherInt is now 3”
從上面這個例子中卖子,我們可以看到 someInt 和 anotherInt 的原始值在 swapTwoInts 函數(shù)中被修改,盡管它們的定義在函數(shù)體外刑峡。
注意: 輸出輸出參數(shù)和返回值是不一樣的洋闽。上面的 swapTwoInts 函數(shù)并沒有定義任何返回值,但仍然修改了 someInt 和 anotherInt 的值突梦。輸入輸出參數(shù)是函數(shù)對函數(shù)體外產(chǎn)生影響的另一種方式诫舅。
, internal,
let, operator:自定義運(yùn)算符, private, protocol, public,
static, struct, subscript, typealias, and var.
Keywords used in statements:
break, case, continue, default, defer, do, else,
fallthrough, for, guard, if, in, repeat, return,
switch, where, and while.
Keywords used in expressions and types:
as, catch, dynamicType, false, is, nil,
rethrows, super, self, Self, throw,
throws, true, try, #column, #file, #function, and #line.
Keywords used in patterns:
_.
Keywords that begin with a number sign (#):
#available, #column, #else#elseif,
#endif, #file, #function, #if, #line, and #selector.
Keywords reserved in particular contexts:
associativity, convenience, dynamic,
didSet, final, get, infix, indirect, lazy,
left, mutating, none, nonmutating,
optional, override, postfix, precedence,
prefix, Protocol, required, right, set,
Type, unowned, weak, and willSet.
Outside the context in which they appear in the grammar, they can be used as identifiers.
The following tokens are reserved as punctuation and can’t be used as custom operators:
(, ), {, }, [, ], ., ,, :, ;, =, @, #,
& (as a prefix operator), ->, `, ?, and ! (as a postfix operator).