Swift 表达式
expression → prefix-expressionbinary-expressionsopt
expression-list → expression expression,
expression-list
很多其它信息,请參见: "In-Out parameters".
prefix-expression → prefix-operatoroptpostfix-expression
prefix-expression → in-out-expression
in-out-expression → &
identifier
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612190526_1.png)
>>
按位右移(Bitwise right shift)binary-expression → binary-operatorprefix-expression
binary-expression → assignment-operatorprefix-expression
binary-expression → conditional-operatorprefix-expression
binary-expression → type-casting-operator
binary-expressions → binary-expressionbinary-expressionsopt
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612190627_1.png)
- (a, _, (b, c)) = ("test", 9.45, (12, 3))
- // a is "test", b is 12, c is 3, and 9.45 is ignored
形式例如以下:
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612190711_1.png)
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612182526_1.png)
- class SomeSuperType {}
- class SomeType: SomeSuperType {}
- class SomeChildType: SomeType {}
- let s = SomeType()
- let x = s as SomeSuperType // known to succeed; type is SomeSuperType
- let y = s as Int // known to fail; compile-time error
- let z = s as SomeChildType // might fail at runtime; type is SomeChildType?
- let y1 = x as SomeType // Type information from 'as'
- let y2: SomeType = x // Type information from an annotation
- "hello" is String
- "hello" is Int
primary-expression → identifiergeneric-argument-clauseopt
primary-expression → literal-expression
primary-expression → self-expression
primary-expression → superclass-expression
primary-expression → closure-expression
primary-expression → parenthesized-expression
primary-expression → implicit-member-expression
primary-expression → wildcard-expression
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612182732_1.png)
在init/subscript中 仅仅有的特殊成员(member)中会返回这个keyword的名字,在某个文件的顶端(the top level of a file),它返回的是当前module的名字。
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612182759_1.png)
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612182810_1.png)
literal-expression → literal
literal-expression → array-literal dictionary-literal
literal-expression → __FILE__
__LINE__
__COLUMN__
__FUNCTION__
array-literal → [
array-literal-itemsopt]
array-literal-items → array-literal-item,
opt array-literal-item,
array-literal-items
array-literal-item → expression
dictionary-literal → [
dictionary-literal-items]
[:]
dictionary-literal-items → dictionary-literal-item,
opt dictionary-literal-item,
dictionary-literal-items
dictionary-literal-item → expression:
expression
它的形式例如以下:
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612182920_1.png)
- class SomeClass {
- var greeting: String
- init(greeting: String) {
- self.greeting = greeting
- }
- }
- struct Point {
- var x = 0.0, y = 0.0
- mutating func moveByX(deltaX: Double, y deltaY: Double) {
- self = Point(x: x + deltaX, y: y + deltaY)
- }
- }
self-expression → self
self-expression → self.
identifier
self-expression → self[
expression]
self-expression → self.init
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612183215_1.png)
superclass-expression → superclass-method-expression superclass-subscript-expression superclass-initializer-expression
superclass-method-expression → super.
identifier
superclass-subscript-expression → super[
expression]
它的形式例如以下:
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612183334_1.png)
- myFunction {
- (x: Int, y: Int) -> Int in
- return x + y
- }
- myFunction {
- (x, y) in
- return x + y
- }
- myFunction { return $0 + $1 }
- myFunction { $0 + $1 }
- myFunction { print(self.title) } // strong capture
- myFunction { [weak self] in print(self!.title) } // weak capture
- myFunction { [unowned self] in print(self.title) } // unowned capture
- // Weak capture of "self.parent" as "parent"
- myFunction { [weak parent = self.parent] in print(parent!.title) }
closure-expression → {
closure-signatureoptstatements}
closure-signature → parameter-clausefunction-resultoptin
closure-signature → identifier-listfunction-resultoptin
closure-signature → capture-listparameter-clausefunction-resultoptin
closure-signature → capture-listidentifier-listfunction-resultoptin
closure-signature → capture-listin
capture-list → [
capture-specifierexpression]
capture-specifier → weak|
unowned
| unowned(safe)|
unowned(unsafe)
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612183551_1.png)
- var x = MyEnumeration.SomeValue
- x = .AnotherValue
形式例如以下:
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612184852_1.png)
比如: (1)的 type是Int, 而不是(Int)
parenthesized-expression → (
expression-element-listopt)
expression-element-list → expression-element | expression-element,
expression-element-list
expression-element → expression | identifier:
expression
比如:以下的代码中,10被传递给x, 20被忽略(译注:好奇葩的语法。。。)
- (x, _) = (10, 20)
- // x is 10, 20 is ignored
_
严格的讲,每一个主要表达式(primary expression)都是一个后缀表达式。
postfix-expression → primary-expression
postfix-expression → postfix-expressionpostfix-operator
postfix-expression → function-call-expression
postfix-expression → initializer-expression
postfix-expression → explicit-member-expression
postfix-expression → postfix-self-expression
postfix-expression → dynamic-type-expression
postfix-expression → subscript-expression
postfix-expression → forced-value-expression
postfix-expression → optional-chaining-expression
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612185017_1.png)
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612185123_1.png)
- // someFunction takes an integer and a closure as its arguments
- someFunction(x, {$0 == 13})
- someFunction(x) {$0 == 13}
- // someFunction takes a closure as its only argument
- myData.someMethod() {$0 == 13}
- myData.someMethod {$0 == 13}
function-call-expression → postfix-expressionparenthesized-expression
function-call-expression → postfix-expressionparenthesized-expressionopttrailing-closure
trailing-closure → closure-expression
它的形式例如以下:
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612185237_1.png)
- var x = SomeClass.someClassFunction // ok
- var y = SomeClass.init // error
- class SomeSubClass: SomeSuperClass {
- init() {
- // subclass initialization goes here
- super.init()
- }
- }
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612185827_1.png)
- class SomeClass {
- var someProperty = 42
- }
- let c = SomeClass()
- let y = c.someProperty // Member access
- var t = (10, 20, 30)
- t.0 = t.1
- // Now t is (20, 20, 30)
explicit-member-expression → postfix-expression.
decimal-digit
explicit-member-expression → postfix-expression.
identifiergeneric-argument-clauseopt
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612185928_1.png)
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612190115_1.png)
- class SomeBaseClass {
- class func printClassName() {
- println("SomeBaseClass")
- }
- }
- class SomeSubClass: SomeBaseClass {
- override class func printClassName() {
- println("SomeSubClass")
- }
- }
- let someInstance: SomeBaseClass = SomeSubClass()
- // someInstance is of type SomeBaseClass at compile time, but
- // someInstance is of type SomeSubClass at runtime
- someInstance.dynamicType.printClassName()
- // prints "SomeSubClass"
它的形式是:
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612190214_1.png)
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612190245_1.png)
' 组成,形式例如以下:
![](http://www.cocoachina.com/cms/uploads/allimg/140612/4196_140612190316_1.png)
' 返回目标表达式的值。把它做为可选的參数传递给兴许的表达式
.property.performAction() 这句代码在运行时,就会先获得c 的property方法。然后调用 performAction()方法。
然后对于 "c?.property.performAction()" 这个总体,它的返回值是一个optional type.
- var c: SomeClass?
- var result: Bool?
= c?.property.performAction()
- if let unwrappedC = c {
- result = unwrappedC.property.performAction()
- }