Custom operators are underrated topic among iOS developers. Some people think they’re bad , whereas others love to use them.
Code can be written in less lines, while keeping it clear what is happening. Custom operators are also known as advanced operators and allow you to combine two instances with a self-chosen infix, prefix and postfix.
Today, I’m going to show you the benefits of custom operators, how they are used in projects.
Types of Operator
There are three different types of operators as following:-
- Unary operators: operators that work on only one operand (e.g., = and !)
- Binary operators: work on two operands, such as + , - , *, etc.
- Ternary operator: works on three operands, e.g., ?: (ternary operator)
Operator notation
Notation defines the position of the operator when used with the operands, there are of three types
- Infix: when the operators are used in between operands. e.g c = a + b where + is the infix operator
- Prefix: when the operators are used before an operand, e.g., ++, --, and !
- Postfix: when the operators are used after an operand, e.g., ! and ? Used for force unwrapping and optional chaining in swift
Operator precedence and associativity in Swift
Operator precedence
When working with operators in Swift, you should also know the priority order in which the operator is executed. Operator precedence only matters for infix operators, as they work on multiple operands.
e.g var ans = 5 + 2 * 4 // 2 * 4 is done first which yields 8, then we do 5 + 8 = 13
In the example, * has more precedence compared to +, which is why 2 * 4 is executed before 5 + 2.
Associativity
Associativity is of two types:
- left: this means that the expression to the left will be resolved first
- right: this means the expression to the right will be resolved first
Commonly user operators
1. Assignment operator
= this symbol is used to assign values to variable as well as constants
For example let a = 5
2. Arithmetic operators
Those type of operators used to perform a basic mathematical operations
e.g +, - , % , * , / and %
3. Logical operators
Those operators used to build a logical expressions
e.g., !, &&, and ||.
4.Comparison operators
Those operators are used to compare 2 values and return the bool depending on whether the comparison is true or not
e.g. >, <, ==, >=, <=, and !=.
5.Ternary operator
Since the Conditional Operator '?:' takes three operands to work, hence they are also called ternary operator. e.g ?: —> 1 < 2 ? print(“1 is less then 2”) : print(“1 is greater then 2”)
6. Nil coalescing operator
This operator is also known as ?? , and it gives the default value to optional arguments E.g. let num: Int? = nil
print(num ?? 0) 0 is the default value
Creating Custom Operators
Now let's create our own custom operators. in this section I will show you how we use operators in projects to make things easier.
Custom Infix Operator
Let’s create a new searching operator using '=?'. in the following example I will show you how to make searching more easier in projects.
Let's Start
we have created a searchable protocol. its a protocol oriented approach which helps to create searching possible in any type.
Now we create a models in which we want to enable searching using custom protocols.
so above we created two models named Student and Employee. as you notice, they inherit our Searchable protocol. so they need to return some unique name in searchableText as it is a getter.
So in the above code, we declared our infix '=?' operator, and defined its associativity to the left so that, if we use the operator chaining left, the problem will be solved first. and given the meaning or logic to operator.
Now Let's Use It
We created bunch of students and employees stored in array.
That's how we use custom infix operator
Custom Postfix Operator
Let’s create a custom object creating postfix operator with '++'
In above code we define a Employee blueprint or protocol with its name and constructor.
now we defined a structure of Employee inheriting EmployeePresentable protocol and it's default case for empty object.
so we defined a postfix operator with its meaning within its function.
As you can see in the above code creation of objects now easy with postfix operator
Custom Prefix Operator
Let’s create a custom object creating prefix operator with '++'
In the above example code, we define prefix operator with its meaning that every object that inherits EmployeePresentable protocol would increase its salary by 10.
Note:- Prefix Operator only take one object as parameter
Conclusion
This brings us to the conclusion of what operators are in Swift and how you can create your own. Although they are pretty simple to use, understanding them is important because they are one of the most foundational constructs of any programming language.
Define them only if the symbol’s original meaning makes sense for you.
Thanks for reading. Happy coding!