super . init
super [
to access the superclass’s subscript implementation. The third form is used to access an initializer of the superclass.
Subclasses can use a superclass expression in their implementation of members, subscripting, and initializers to make use of the implementation in their superclass.
G R A M M A R O F A S U P E R C L A S S E X P R E S S I O N
superclass-expression → superclass-method-expression superclass-subscript- expression superclass-initializer-expression
superclass-method-expression → super . identifier superclass-subscript-expression → expression ] superclass-initializer-expression →
Closure Expression
A closure expression creates a closure, also known as a lambda or an anonymous function in other programming languages. Like function declarations, closures contain statements which they execute, and they capture values from their enclosing scope. It has the following form:
{ ( parameters ) -> return type in
statements
}
The parameters have the same form as the parameters in a function declaration, as described in Function Declaration.
There are several special forms that allow closures to be written more concisely:
A closure can omit the types of its parameters, its return type, or both. If you omit the parameter names and both types, omit the in keyword before the statements. If the omitted types can’t be inferred, a compile-time error is raised.
A closure may omit names for its parameters. Its parameters are then implicitly named $ followed by their position: $0, $1, $2, and so on.
A closure that consists of only a single expression is understood to return the value of that expression. The contents of this expression is also considered when performing type inference on the surrounding expression.
The following closure expressions are equivalent:
myFunction {
(x: Int, y: Int) -> Int in
return x + y
}
myFunction {
(x, y) in
return x + y
}
myFunction { return $0 + $1 }
myFunction { $0 + $1 }
For information about passing a closure as an argument to a function, see Function Call Expression.
A closure expression can explicitly specify the values that it captures from the surrounding scope using a capture list. A capture list is written as a comma separated list surrounded by square brackets, before the list of parameters. If you use a capture list, you must also use the in keyword, even if you omit the parameter names, parameter types, and return type.
Each entry in the capture list can be marked as weak or unowned to capture a weak or unowned reference to the value.
myFunction { print(self.title) } |
// strong capture |
myFunction { [weak self] in print(self!.title) } |
// weak capture |
myFunction { [unowned self] in print(self.title) } // unowned capture
You can also bind arbitrary expression to named values in the capture list. The expression is evaluated when the closure is formed, and captured with the specified strength. For example:
// Weak capture of "self.parent" as "parent"
myFunction { [weak parent = self.parent] in print(parent!.title) }
For more information and examples of closure expressions, see Closure Expressions.
G R A M M A R O F A C L O S U R E E X P R E S S I O N
closure-expression → { closure-signature opt statements }
closure-signature → parameter-clause function-result opt in closure-signature → identifier-list function-result opt in closure-signature → capture-list parameter-clause function-result opt in closure-signature → capture-list identifier-list function-result opt in closure-signature → capture-list in
capture-list → [ capture-specifier expression ]
capture-specifier → weak unowned unowned(safe) unowned(unsafe)
Implicit Member Expression
An implicit member expression is an abbreviated way to access a member of a type, such as an enumeration case or a class method, in a context where type inference can determine the implied type. It has the following form:
. member name
For example:
var x = MyEnumeration.SomeValue
x = .AnotherValue
G R A M M A R O F A I M P L I C I T M E M B E R E X P R E S S I O N
implicit-member-expression → . identifier
Parenthesized Expression
A parenthesized expression consists of a comma-separated list of expressions surrounded by parentheses. Each expression can have an optional identifier before it, separated by a colon (:). It has the following form:
( identifier 1 : expression 1 , identifier 2 : expression 2 , ... )
Use parenthesized expressions to create tuples and to pass arguments to a function call. If there is only one value inside the parenthesized expression, the type of the parenthesized expression is the type of that value. For example, the type of the parenthesized expression (1) is Int, not (Int).
G R A M M A R O F A P A R E N T H E S I Z E D E X P R E S S I O N
parenthesized-expression → ( expression-element-list opt ) expression-element-list → expression-element expression-element , expression-
element-list
expression-element → expression identifier : expression
Wildcard Expression
A wildcard expression is used to explicitly ignore a value during an assignment. For example, in the following assignment 10 is assigned to x and 20 is ignored:
(x, _) = (10, 20)
// x is 10, 20 is ignored
G R A M M A R O F A W I L D C A R D E X P R E S S I O N
wildcard-expression → _
Postfix Expressions
Postfix expressions are formed by applying a postfix operator or other postfix syntax to an expression. Syntactically, every primary expression is also a postfix expression.
The Swift standard library provides the following postfix operators:
++ Increment -- Decrement
For information about the behavior of these operators, see Basic Operators and Advanced Operators.
G R A M M A R O F A P O S T F I X E X P R E S S I O N
postfix-expression → primary-expression postfix-expression → postfix-expression postfix-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
Function Call Expression
A function call expression consists of a function name followed by a comma-separated list of the function’s arguments in parentheses. Function call expressions have the following form:
function name ( argument value 1 , argument value 2 )
The function name can be any expression whose value is of a function type.
If the function definition includes names for its parameters, the function call must include names before its argument values separated by a colon (:). This kind of function call expression has the following form:
function name ( argument name 1 : argument value 1 , argument name 2 : argument value 2 )
A function call expression can include a trailing closure in the form of a closure expression immediately after the closing parenthesis. The trailing closure is understood as an argument to the function, added after the last parenthesized argument. The following function calls are equivalent:
// someFunction takes an integer and a closure as its arguments
someFunction(x, {$0 == 13})
someFunction(x) {$0 == 13}
If the trailing closure is the function’s only argument, the parentheses can be omitted.
// someFunction takes a closure as its only argument
myData.someMethod() {$0 == 13}
myData.someMethod {$0 == 13}
G R A M M A R O F A F U N C T I O N C A L L E X P R E S S I O N
function-call-expression → postfix-expression parenthesized-expression function-call-expression → postfix-expression parenthesized-expressionopt trailing-
closure
trailing-closure → closure-expression
Initializer Expression
An initializer expression provides access to a type’s initializer. It has the following form:
expression .init( initializer arguments )
You use the initializer expression in a function call expression to initialize a new instance of a type. Unlike functions, an initializer can’t be used as a value. For example:
var x = SomeClass.someClassFunction // ok
var y = SomeClass.init |
// error |
You also use an initializer expression to delegate to the initializer of a superclass.
class SomeSubClass: SomeSuperClass {
init() {
// subclass initialization goes here
super.init()
}
}
G R A M M A R O F A N I N I T I A L I Z E R E X P R E S S I O N
initializer-expression → postfix-expression . init
Explicit Member Expression
A explicit member expression allows access to the members of a named type, a tuple, or a module. It consists of a period (.) between the item and the identifier of its member.
expression . member name
The members of a named type are named as part of the type’s declaration or extension. For example:
class SomeClass {
var someProperty = 42
}
let c = SomeClass()
let y = c.someProperty // Member access
The members of a tuple are implicitly named using integers in the order they appear, starting from zero. For example:
var t = (10, 20, 30)
t.0 = t.1
// Now t is (20, 20, 30)
The members of a module access the top-level declarations of that module.
G R A M M A R O F A N E X P L I C I T M E M B E R E X P R E S S I O N
explicit-member-expression → postfix-expression . explicit-member-expression → postfix-expression .
clause opt
decimal-digit
identifier generic-argument-
Postfix Self Expression
A postfix self expression consists of an expression or the name of a type, immediately followed by .self. It has the following forms:
expression .self
type .self
The first form evaluates to the value of the expression. For example, x.self evaluates to
x.
The second form evaluates to the value of the type. Use this form to access a type as a value. For example, because SomeClass.self evaluates to the SomeClass type itself, you can pass it to a function or method that accepts a type-level argument.
G R A M M A R O F A S E L F E X P R E S S I O N
postfix-self-expression → postfix-expression . self
Dynamic Type Expression
A dynamicType expression consists of an expression, immediately followed by .dynamicType. It has the following form:
expression .dynamicType
The expression can’t be the name of a type. The entire dynamicType expression evaluates to the value of the runtime type of the expression, as the following example shows:
1 class SomeBaseClass {
2class func printClassName() {
3println("SomeBaseClass")
4}
5 }
6 class SomeSubClass: SomeBaseClass {
7override class func printClassName() {
8println("SomeSubClass")
9}
tsomeInstance: SomeBaseClass = SomeSubClass()
/someInstance is of type SomeBaseClass at compile time, but
/someInstance is of type SomeSubClass at runtime omeInstance.dynamicType.printClassName()
/prints "SomeSubClass"
G R A M M A R O F A D Y N A M I C T Y P E E X P R E S S I O N
dynamic-type-expression → postfix-expression . dynamicType
Subscript Expression
A subscript expression provides subscript access using the getter and setter of the corresponding subscript declaration. It has the following form:
expression [ index expressions ]
To evaluate the value of a subscript expression, the subscript getter for the expression’s type is called with the index expressions passed as the subscript parameters. To set its value, the subscript setter is called in the same way.
For information about subscript declarations, see Protocol Subscript Declaration.
G R A M M A R O F A S U B S C R I P T E X P R E S S I O N
subscript-expression → postfix-expression [ expression-list ]
Forced-Value Expression
A forced-value expression unwraps an optional value that you are certain is not nil. It has the following form:
expression !
If the value of the expression is not nil, the optional value is unwrapped and returned with the corresponding nonoptional type. Otherwise, a runtime error is raised.
G R A M M A R O F A F O R C E D - V A L U E E X P R E S S I O N
forced-value-expression → postfix-expression !
Optional-Chaining Expression
An optional-chaining expression provides a simplified syntax for using optional values in postfix expressions. It has the following form:
expression ?
On its own, the postfix ? operator simply returns the value of its argument as an optional.
Postfix expressions that contain an optional-chaining expression are evaluated in a special way. If the optional-chaining expression is nil, all of the other operations in the postfix expression are ignored and the entire postfix expression evaluates to nil. If the optional-chaining expression is not nil, the value of the optional-chaining expression is unwrapped and used to evaluate the rest of the postfix expression. In either case, the value of the postfix expression is still of an optional type.
If a postfix expression that contains an optional-chaining expression is nested inside other postfix expressions, only the outermost expression returns an optional type. In the example below, when c is not nil, its value is unwrapped and used to evaluate both
.property and .performAction(), and the entire expression c?.property.performAction() has a value of
an optional type.
var c: SomeClass?
var result: Bool? = c?.property.performAction()
The following example shows the behavior of the example above without using optional chaining.
if let unwrappedC = c {
result = unwrappedC.property.performAction()
}
G R A M M A R O F A N O P T I O N A L- C H A I N I N G E X P R E S S I O N
optional-chaining-expression → postfix-expression ?