keywords and identifiers


Keywords used in declarations:

class, deinit, enum, extension, func, import, init, inout, internal, let, operator, private, protocol, public, static, struct, subscript, typealias, var

Keywords used in statements:

break, case, continue, default, defer, do, else, fallthrough, for, guard, if, in, repeat, return, switch, where, while

Keywords used in expressions and types:

as, catch, dynamicType, false, is, nil, rethrows, super, self, Self, throw, throws, true, try, __COLUMN__, __FILE__, __FUNCTION__, __LINE__

Keywords used in patterns:

_

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, 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)

关键字和符号


下面这些被保留的关键字(keywords)不允许用作标识符,除非被反引号转义,具体描述请参考 标识符。

  • 用在声明中的关键字:
classdeinitenumextensionfuncimportinitletprotocolstaticstructsubscripttypealiasvar
  • 用在语句中的关键字:
breakcasecontinuedefaultdoelsefallthroughifinforreturnswitchwherewhile
  • 用在表达式和类型中的关键字:
asdynamicTypeisnewsuperselfSelfType__COLUMN____FILE____FUNCTION____LINE__
  • 用在模式中的关键字:
_
  • 特定上下文中被保留的关键字:
associativitydidSetgetinfixinoutleftmutatingnonenonmutatingoperatoroverridepostfixprecedenceprefixrightsetunownedunowned(safe)unowned(unsafe)weakwillSet

这些关键字在特定上下文之外可以被用于标识符。

  • 以下标记被当作保留符号,不能用于自定义操作符:
 (  )  {  }  [  ]  .  ,  :  ;  =  @  #  &(作为前缀操作符)  -> 、``  ?  !(作为后缀操作符)`。