Introduction
As a developer, you always try to make your code more and more compact and with the same functionality, and sometimes you want to combine many features and add your features multiple times, but how do you achieve that in swift?
The answer is with the use of classes and structures. They both have the same functionalities, the only difference being classes are reference types and whereas structures are value types. One of the classes is that singleton is mainly used today. And for composite values, we can use patterns, and we will learn all about patterns and singleton class while moving further in this blog so let's move on without wasting anytime further.
Patterns and their Types
The pattern represents the structure of a composite value or a single value. In swift, there usually are two kinds of patterns, one which does not match any type of value and the other that matches any kind of value.
The first type of pattern is used for full pattern matching, and it is also useful when the values you are trying to find are not present in the runtime. These include type-casting, expression, optional, and enumeration case patterns. You can use these patterns in the do statement, switch statement, or in the case of if statement.
The second type of pattern is used for destructing values in a constant, simple variable, and optional bindings. These include any kind of tuple, identifier, and wildcard patterns.
Wildcard Pattern
A Wildcard pattern ignores and matches any value and consists of an underscore(_). Use a wildcard pattern when you don't care about the values being matched against. The following code, for example, iterates through the closed range 1...3, ignoring the current value of the range on each loop iteration:
for _ in 1...3 {
// Do something three times.
}
Identifier Pattern
It matches any value, and if the value matches, it binds the matched value to a constant name or a variable.
As in the example below, Someval is an identifier pattern that matches the value 22. IF the matching succeeds, 22 is assigned or bound to the constant name Someval:
let Someval = 22
Value-Binding Pattern
Value-binding patterns associate matching values with variable or constant names. The let keyword binds a matched value to a constant name, while the var keyword is used to bind to the name of a variable. Identifier patterns used within the value-binding pattern:
let point = (3, 2)
switch point {
// Bind x and y to the elements of point.
case let (x, y):
print("The given point is at (\(x), \(y)).")
}
Tupple Pattern
A tupple pattern is a list of zero or more patterns enclosed in parenthesis separated by a comma(,). When a tuple pattern is used in the constant declaration or variable declaration, it can contain only identifier patterns, wildcard patterns, optional patterns, or any other tuple patterns that have those patterns. For example, the below code is not valid as (x,0) is an expression pattern.
let points = [(3, 0), (4, 0), (5, 1), (6, 0), (6, 1)]
for (x, 0) in points {
/* ... */
}
Enumeration Case Pattern
In the Enumeration case pattern, we match the case of the existing enumeration type. They appear in switch statements, if, guard, while, and for in statements. It also matches the value that is wrapped in an optional. YOu must specify a tuple that contains a value that has an element for each associated value.
enum SomeEnum { case left, right }
let x: SomeEnum? = .left
switch x {
case .right:
print("Turn right")
case .left:
print("Turn left")
case nil:
print("Keep going straight")
}
Optional Pattern
The optional pattern matches the values of the optional enumeration. They use an identifier pattern followed by a question mark(?) and appear similar to the enumeration pattern.
let someOptional: Int? = 42
// Match using an enumeration case pattern.
if case .some(let x) = someOptional {
print(x)
}
Type-Casting Pattern
There are two types of type casting patterns one is a pattern, and the other is a pattern. The pattern type matches the same value as that of the specified value on the right side of the pattern at run time similarly, as the pattern matches the value with the right side specified value of as pattern at run time.