Recommended Reading:IOS APP architecture design
Patterns represent structures of single or combined values. For example, the structure of a tuple (1,2) is a list of two elements separated by a comma. Because a pattern represents a structure of values, rather than any one particular value, you can match it with multiple values. For example, the pattern (x,y) can match tuples (1,2) and any other tuple of two elements. Instead of matching a value to a pattern, you can extract some or all of the combined values and bind each part to a constant or variable.
In Swift, patterns appear in the declarations of variables and constants (to the left of them), in for-in statements and switch statements (in their case tags). While patterns can appear in any case tag in a switch statement, in other cases only wildcard patterns, identifier patterns, and patterns containing both of them can appear.
You can specify a type annotation for wildcard patterns, identifier patterns, and tuple patterns to constrain patterns to match only values that match a particular type.
Pattern syntax pattern → identifier- PatternType – Annotationopt pattern → value-binding-pattern pattern → Patterntype-annotationopt pattern → enum-case-pattern pattern → type-casting-pattern pattern → expression-pattern
Wildcard pattern
As a developer, it is important to have a learning atmosphere and a communication circle. No matter you are a small white or a big ox, you are welcome to join us. Let’s make progress together and develop together! Click to get: Interview Materials
Wildcard patterns match and ignore any values, including the underscore (_). Wildcard patterns can be used when you don’t care about the values being matched. For example, the following code is in the closed interval 1… And ignore the current value of the interval at each iteration:
1. for _ in 1... 3 {2. // Execute three times 3.}Copy the code
Syntax for wildcard pattern wildcard-pattern → _
Identifier pattern
The identifier pattern matches any value and binds the matched value to a variable or constant. For example, in the following constant declaration, someValue is an identifier pattern that matches an Int with a value of 42.
1. let someValue = 42
When the match is successful, 42 is bound (assigned) to the constant someValue.
When the pattern on the left of a variable or constant declaration is an identifier pattern, the identifier pattern is an implicit value binding pattern.
Syntax identifier-pattern → identifier for the identifier pattern
Value binding mode
Value binding mode binds a matched value to a variable or constant. When binding a matching value to a constant name, the value binding mode starts with the keyword let; When binding a value to a variable name, it starts with the keyword var.
The identifier pattern is contained in the value binding pattern to bind newly named variables or constants to the values they match. For example, you can decompose the elements of a tuple and bind the value of each element to the corresponding identifier pattern.
1. Let (x, y) = (3, 2) 2. Switch point {3. 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 example above, let assigns elements of the tuple schema (x, y) to individual identifier schemas. Because of this behavior, the switch statement case let (x, y): and case (let x, let y): match the same value.
Syntax for value binding patterns: value-binding-pattern → varpattern letpattern
A tuple pattern
A tuple pattern is a comma-separated list of zero or more patterns enclosed in parentheses. Tuple patterns match values 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) only matches tuples where both elements are ints. If you only need to restrict some elements in the tuple schema, you can provide type annotations directly 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 a tuple pattern is used as a for-in statement or as a pattern for variable or constant declarations, it can contain only wildcard patterns, identifier patterns, or other patterns that contain 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 a tuple pattern containing only a single element do not work. The pattern matches the value of a single element type. For example, the following 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
Enumeration instance pattern
The enumeration instance pattern matches an instance of an existing enumeration type. The enumeration instance pattern appears only in the case label of the switch statement.
If the enumeration instance you are going to match has any associated values, then the corresponding enumeration instance pattern must specify a tuple pattern that contains the elements of each associated value. Use the switch statement to match an example that contains an enumeration instance of Associated Values, see Associated Values.
Enum -case-pattern → type-identifier opt. enum-case-name tuple-pattern opt
Type conversion mode
There are two types of conversion modes: IS mode and AS mode. Both modes appear only in the case label in the switch statement. The IS and AS modes have the following forms:
1. is type
2. pattern as type
Copy the code
If the type of a value is the same at runtime as the specified type to the right of the IS pattern or a subclass of the specified type, then the IS pattern matches the value. The IS mode behaves like the IS operator. They both cast, but discard the return type.
If the type of a value is the same at runtime as the specified type or subclass of the specified type to the right of the AS pattern, then the AS pattern matches the value. If the match is successful, the type of the matched value is converted to the pattern specified to the left of the AS pattern.
See Type Casting for Any and AnyObject for an example of using switch statements to match values that use is and AS patterns.
Syntax of type casting-pattern → IS-pattern as-pattern IS-pattern → istype as-pattern → Patternastype
Expression pattern
An expression pattern represents the value of an expression. Expression patterns only appear in case labels in switch statements.
The expressions represented by the expression pattern are compared to the value of the input expression using the ~= operator in the Swift standard library. If the = operator returns true, the match succeeds. 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 custom expression matching behavior. For example, you can rewrite the example above 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
Expression -pattern → expression
Recommended Reading:IOS APP architecture design
This article series will continue to be updated, and you can also send me a personal message to get your interview information. If you have any comments and suggestions, please leave me a message.
Please pay attention to iOS friends! Like words to a like it! Thank you very much! Thank you very much! Thank you very much!
Features: blog.csdn.net/iOS\_asuka/…