Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

the-swift-rogramming-language

.pdf
Скачиваний:
13
Добавлен:
11.05.2015
Размер:
4.27 Mб
Скачать

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-expressionpostfix-expression parenthesized-expression function-call-expressionpostfix-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:

class SomeBaseClass {

class func printClassName() {

println("SomeBaseClass")

}

}

class SomeSubClass: SomeBaseClass { override class func printClassName() {

println("SomeSubClass")

}

meInstance: SomeBaseClass = SomeSubClass() meInstance is of type SomeBaseClass at compile time, but meInstance is of type SomeSubClass at runtime Instance.dynamicType.printClassName()

ts "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-expressionpostfix-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 ?

Statements

In Swift, there are two kinds of statements: simple statements and control flow statements. Simple statements are the most common and consist of either an expression or a declaration. Control flow statements are used to control the flow of execution in a program. There are three types of control flow statements in Swift: loop statements, branch statements, and control transfer statements.

Loop statements allow a block of code to be executed repeatedly, branch statements allow a certain block of code to be executed only when certain conditions are met, and control transfer statements provide a way to alter the order in which code is executed. Each type of control flow statement is described in detail below.

A semicolon (;) can optionally appear after any statement and is used to separate multiple statements if they appear on the same line.

G R A M M A R O F A S T A T E M E N T

statement → expression ;opt statement → declaration ;opt statement → loop-statement ;opt statement → branch-statement ;opt statement → labeled-statement

statement → control-transfer-statement ;opt statements → statement statements opt

Loop Statements

Loop statements allow a block of code to be executed repeatedly, depending on the conditions specified in the loop. Swift has four loop statements: a for statement, a for-in statement, a while statement, and a do-while statement.

Control flow in a loop statement can be changed by a break statement and a continue statement and is discussed in Break Statement and Continue Statement below.

G R A M M A R O F A L O O P S T A T E M E N T

loop-statement → for-statement loop-statement → for-in-statement

loop-statement → while-statement loop-statement → do-while-statement

For Statement

A for statement allows a block of code to be executed repeatedly while incrementing a counter, as long as a condition remains true.

A for statement has the following form:

for initialization ; condition ; increment {

statements

}

The semicolons between the initialization, condition, and increment are required. The braces around the statements in the body of the loop are also required.

A for statement is executed as follows:

1.The initialization is evaluated only once. It is typically used to declare and initialize any variables that are needed for the remainder of the loop.

2.The condition expression is evaluated.

If true, the program executes the statements, and execution continues to step 3. If false, the program does not execute the statements or the increment expression, and the program is finished executing the for statement.

3. The increment expression is evaluated, and execution returns to step 2.

Variables defined within the initialization are valid only within the scope of the for statement itself.

The value of the condition expression must have a type that conforms to the LogicValue protocol.

G R A M M A R O F A F O R S T A T E M E N T

 

for-statement

for

for-init opt ;

expressionopt ; expressionopt code-block

for-statement

for

( for-init opt

; expressionopt ; expressionopt ) code-block

for-initvariable-declaration

expression-list

For-In Statement

A for-in statement allows a block of code to be executed once for each item in a collection (or any type) that conforms to the Sequence protocol.

A for-in statement has the following form:

for item in collection { statements

}

The generate method is called on the collection expression to obtain a value of a generator type—that is, a type that conforms to the Generator protocol. The program begins executing a loop by calling the next method on the stream. If the value returned is not None, it is assigned to the item pattern, the program executes the statements, and then continues execution at the beginning of the loop. Otherwise, the program does not perform assignment or execute the statements, and it is finished executing the for-in statement.

G R A M M A R O F A F O R- I N S T A T E M E N T

for-in-statementfor pattern in expression code-block

While Statement

A while statement allows a block of code to be executed repeatedly, as long as a condition remains true.

A while statement has the following form:

while condition {

statements

}

A while statement is executed as follows:

1.The condition is evaluated.

If true, execution continues to step 2. If false, the program is finished executing the while statement.

2.The program executes the statements, and execution returns to step 1.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]