A pattern represents a structure of single or combined values. For example, the structure of a tuple (1,2) is a comma-separated list of two elements. Because a pattern represents a structure of values, rather than any one particular value, you can match it with multiple values. For example, patterns (x,y) can match tuples (1,2) and any other tuple of two elements. In addition to matching a value to a pattern, you can extract some or all of the combined values and then bind the parts to constants or variables.

In Swift, patterns appear in declarations of variables and constants (to the left of them), in for-in statements, and in switch statements (in their case tags). Although patterns may appear in any case tag of a Switch statement, in other cases only wildcard patterns, identifier patterns, and patterns containing both can appear.

You can specify a type annotation for the wildcard pattern, identifier pattern, and tuple pattern to constrain the pattern to match only values that match a particular type.

Patterntype-annotationopt pattern → value-binding-pattern pattern → Tuple – Patterntype-annotationOpt pattern → enum-case-pattern pattern → type-casting-pattern pattern → expression-pattern

Wildcard mode

The wildcard pattern matches and ignores any value, including underscore (_). Wildcard patterns are used when you don’t care about matching values. For example, the following code closes interval 1… 3, and ignore the current value of the interval during each iteration:

1. for _ in 1... 3 {2. // Execute three times 3.}Copy the code

The syntax of wildcard pattern is wildcard-pattern → _

Identifier pattern

As a developer, it is particularly important to have a learning atmosphere and a communication circle. This is my iOS development public number: Programming Daxin, whether you are small white or big ox are welcome to enter. Let us progress together and develop together! (The group will provide some free study books collected by the group owner and hundreds of interview questions and answers documents for free!)

The identifier pattern matches any value and binds the matched value to a variable or constant. For example, in the constant declaration below, someValue is an identifier pattern that matches the value 42 of Int.


1.  let someValue = 42
Copy the code

When the match is successful, 42 is bound (assigned) to the constant someValue.

When the pattern to the left of a variable or constant declaration is an identifier pattern, the identifier pattern is an implicit value binding pattern.

Syntax for identifier patterns – Pattern → identifier

Value binding mode

The value binding mode binds the matched value to a variable or constant. When binding a matching value to a constant name, the value binding mode begins with the keyword let; When binding a value to a variable name, it begins with the keyword var.

The identifier pattern consists of binding newly named variables or constants to their matching values in the value binding pattern. For example, you can decompose the elements of a tuple and bind the values of each element to the corresponding identifier pattern.

Let point = (3, 2) 2. Switch point {3. // bind x and y to point element 4. 5. Println (" The point is The at (, (x), and (y)). ") 6.} 7. / / print "The point is The at (3, 2)."Copy the code

In the above example, let assigns elements of the tuple pattern (x, Y) to each identifier pattern. Because of this behavior, the switch statement case let (x, y): matches the same value as case (let x, let y):.

Syntax for value binding mode value-binding-pattern → varPattern letpattern

A tuple pattern

A tuple pattern is a comma-separated list of zero or more patterns, enclosed in parentheses. The tuple pattern matches the value of the corresponding tuple type.

You can restrict a tuple pattern to match a particular kind of tuple type by using type annotations. For example, the tuple pattern (x, y): (Int, Int) in the constant declaration let (x, y): (Int, Int) = (1, 2) matches only two tuples of type Int. If you only need to restrict some elements in the tuple schema, you can simply provide type annotations to those individual elements. For example, the tuple pattern in let (x: String, y) matches any tuple type that contains two elements and the first element is String.

When tuple mode is used as a mode for for-in statements or for variable or constant declarations, it can contain only wildcard mode, identifier mode, or any other mode containing both. For example, the following code is illegal because element 0 in (x, 0) is an expression pattern:

1. Let points = [(0, 0), (1, 0), (1, 1), (2, 0), (2, 1)] 2. / / the code below is not legitimate 3. For (x, 0) points in {4. / *... * / 5.}Copy the code

Parentheses around tuple patterns containing only a single element do not work. The pattern matches the value of a single element type. For example, the following statement is equivalent:


1.  let a = 2        // a: Int = 2
2.  let (a) = 2      // a: Int = 2
3.  let (a): Int = 2 // a: Int = 2
Copy the code

Tuple syntax tuple pattern – the pattern – (tuple – the pattern – element – the list opt) tuple – the pattern – element – the list – a tuple element – the pattern – | Tuple-pattern-element, tuple-pattern-element-list tuple-pattern-element → pattern

Enumerated instance pattern

Enumeration instance pattern matches an instance of an existing enumeration type. The enumerated instance pattern appears only in the case tag of a Switch statement.

If the enumeration instance you are going to match has any associated value, the corresponding enumeration instance pattern must specify a tuple pattern containing each associated value element. For an example of using a switch statement to match an enumeration instance containing an Associated value, see Associated Values.

Syntax for enumeration instance modes enum-case-pattern → type-identifier opt. enum-case-name tuple-pattern opt

Type conversion mode

There are two types of type conversion modes, IS mode and AS mode. Both modes only appear in the case tag of a switch statement. The IS and AS modes have the following forms:


1.  is type
2.  pattern as type
Copy the code

If a value has the same type at run time as the specified type or a subclass of the specified type on the right side of the IS pattern, then the IS pattern matches the value. The IS mode behaves just like the IS operator; they both perform type conversions but discard the return type.

If a value’s type at runtime is the same as the specified type or a subclass of the specified type on the right side of the AS pattern, then the AS pattern matches the value. If the match is successful, the type of the matching value is converted to the pattern specified on the left side of the AS pattern.

For an example of matching values using is and AS patterns using the switch statement, see Type Casting for Any and AnyObject.

Syntax for type-casting-pattern → IS-Pattern AS-pattern IS-pattern → istype as-pattern → Patternastype

Expression pattern

The expression pattern represents the value of an expression. The expression pattern only appears in the case tag of a switch statement.

The expression represented by the expression pattern is compared with the value of the input expression by using the ~= operator in the Swift standard library. If the = operator returns true, the match is successful. By default, the = operator uses the == operator to compare two values of the same type. It can also match an integer value to a Range of integers in a Range object, as shown in the following example:

1. let point = (1, 2) 2. switch point { 3. case (0, 0): 4. println("(0, 0) is at the origin.") 5. case (-2... 2, 2... 2): 6. println("(\(point.0), \(point.1)) is near the origin.") 7. default: Println ("The point is at (\(point.0), \(point.1)).") 9.} 10.Copy the code

You can override the ~= operator to provide matching behavior for custom expressions. For example, you could rewrite the above example to compare a point expression with a string representation of a point.

2. Func ~=(pattern: String, value: Int) -> Bool { 3. return pattern == "\(value)" 4. } 5. switch point { 6. case ("0", "0"): 7. println("(0, 0) is at the origin.") 8. case ("-2... 2 - ", "2... 2"): 9. println("(\(point.0), \(point.1)) is near the origin.") 10. default: Println ("The point is at (\(point.0), \(point.1)).") 12.} 14.Copy the code

Syntax for expression patterns expression-pattern → expression