If you are looking for an iOS programming language. So Swift is the best you should not miss. Firstly, Swift is the programming language for iOS app development. Secondly, it is a product of Apple which was released in 2014. Thirdly, Swift works well with applications of Apple. For example, iOS, macOS, watchOS and tvOS. Furthermore, it also comes with the open-source LLVM compiler framework. Therefore, Swift becomes the favorite iOS programming language for many application developers. Additionally, Swift has various amazing features for iOS app development. Thus, in this article, the ArrowHiTech team will continue the series blogs about Swift to help you know more about this powerful programming language. Thus now we will introduce the Swift operators: A brief overview for iOS developers. So let’s go.
Why should use iOS programming language- Swift?
Swift is a new programming language compared to others. However, it has a lots of success in iOS app development. As a result, many well-known companies in the world have put their faith in and chosen this language for application development. For example, LinkedIn, Pandora, Vimeo, Twitter, and Groupon. So, take a glance at some Swift’s great features:
- Firstly, it is modern. Swift is the latest programming language. And it is developed by Apple. As a result, it gained lots of reputation from that. Furthermore, Swift code does not require a lot of work from developers Because Swift codes are very like to plain text in English. Therefore, finding out the error code is much easier.
- Secondly is security. You want to be certain that your mobile app is secure and safe. So Swift is the programming language of choice for you. Because this programming language will limit the number of classes of dangerous code. Therefore, Swift offers built-in protection against a wide range of programming faults.
- Thirdly, it is a high-speed. Because Swift combines two files into a single code file. So it is faster than Objective-C. As a result, apps written in Swift will be 3.9 times faster than those written in Python. Thus, it’s a good thing that developers prefer Swift to its competitors.
- Fourthly, there is a reduction in the amount of coding required. Because semicolons and brackets are no longer required. Therefore, you may also merge two strings by using the “+” sign. Hence, Swift code is simpler for you to write.
- Finally, Swift is a free programming language. As a result, there is no fee for using this. However, you still can enjoy all of its fantastic features. Thus you can save a lot of money on iOS app development.
What are Swift operators?
Firstly, Swift operators are symbols that are used to perform operations on variables and values. For example:
So as you can see in the above example, the operator “+” is used to add two numbers: 5 and 6.
Additionally, Objective-C has a large number of built-in operators, including the following:
- Firstly, they are Arithmetic Operators
- Secondly, they are Comparison Operators
- Thirdly, they are Logical Operators
- Fourthly, Bitwise Operators
- Assignment Operators
- Range Operators
- Finally, they are other Swift Operators
What are the types of swift operators?
Arithmetic Swift Operators
Mathematical operations such as addition, subtraction, and multiplication are performed using Arithmetic Swift Operators. Thus, consider the following illustration:
So, from the above example, we can see that the arithmetic operator “-” is used to subtract two numbers or variables.
Therefore, the table below lists all of the arithmetic operators that Swift 4 supports:
NOTE: Only integers are used with the Modulo operator.
var a = 7
var b = 2
print (a + b)
print (a – b)
print (a * b)
Assignment Swift Operators
By using Swift assignment operators, variables are assigned values. For example:
“=” is an assignment operator. Because it assigns 5 to x in this case. Moreover, Swift 4 supports the following list of the many assignment swift operators:
|=||Assignment Operator||a = 7|
|+=||Addition Assignment||a += 1 // a = a + 1|
|-=||Subtraction Assignment||a -= 3 // a = a – 3|
|*=||Multiplication Assignment||a *= 4 // a = a * 4|
|/=||Division Assignment||a /= 3 // a = a / 3|
|%=||Remainder Assignment||a %= 10 // a = a % 5|
// assign 10 to a
var a = 10
// assign 5 to b
var b = 5
// assign the sum of a and b to a
a += b // a = a + b
Comparison Swift Operators
When two values/variables are compared, a boolean result is returned: true or false. For example:
The “>” comparison swift operators used to determine whether or not a is greater than b.
Therefore, the table below lists all of the relational operators that Swift 4 supports:
|==||Is Equal To||4 == 6 gives us false|
|!=||Not Equal To||4 != 6 gives us true|
|>||Greater Than||4 > 6 gives us false|
|<||Less Than||4 < 6 gives us true|
|>=||Greater Than or Equal To||4 >= 6 give us false|
|<=||Less Than or Equal To||4 <= 6 gives us true|
NOTE: In decision-making and loops, comparison operators are utilized.
var a = 5, b = 2
// equal to operator
print(a == b)
// not equal to operator
print(a != b)
// greater than operator
print(a > b)
// less than operator
print(a < b)
// greater than or equal to operator
print(a >= b)
// less than or equal to operator
print(a <= b)
To determine whether an expression is true or false, we should use Logical Swift Operators. Because they are utilized to make decisions. For example:
As you can see, the logical operator AND is used here. Therefore, the result is true because both a > 2 and b >= 6.
|&&||x && y||Logical AND: true only if both the operands are true|
|||||x || y||Logical OR: true if at least one of the operands is true|
|!||!x||Logical NOT: true if the operand is false and vice-versa.|
// logical AND
print(true && true) // true
print(true && false) // false
// logical OR
print(true || false) // true
// logical NOT
print(!true) // false
Firstly, Bitwise Swift Operators are used to do operations on single bits.
|~||Binary One’s Complement|
|<<||Binary Shift Left|
|>>||Binary Shift Right|
Assume A = 60; and B = 13;
So in binary format, they will be as follows:
A = 0011 1100
B = 0000 1101
A & B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
However, Bitwise Swift Operators are seldom used in everyday programming
Swift 4 has two range operators. Because they are shortcuts for expressing a set of values. Additionally, these two operators are explained in the table below.
|Closed Range||(a…b): range that includes the values a and b and runs from a to b.||1…5 gives 1, 2, 3, 4 and 5|
|Half-Open Range||(a..< b): range that runs from a to b, but does not include b.||1..< 5 gives 1, 2, 3, and 4|
|One- sided Range||a… : range that runs from a to end of elements|
…a: defines a range starting from start to a
|1… gives 1 , 2,3… end of elements|
…2 gives beginning… to 1,2
Other Swift Operators
So other operators available in Swift listed below:
|? :||Ternary Operator – returns value based on the condition||(5 > 2) ? “Success” : “Error” // Success|
|??||Nil-Coalescing Operator – checks whether an optional contains a value or not||number ?? 5|
|…||Range Operator – defines range containing values||1…3 // range containing values 1,2,3|
In this blog, we have already covered the brief overview of Swift operators for iOS developers. Furthermore, all types of Swift operators are shown clearly in this article. Moreover, this blog has full example input and output for you to read and understand with ease. However, if you want to dig more information about Swift. So please check out the following articles:
Or in case you want to know more about other Swift information, please access our blog at ArrowHiTech website. Because in ArrowHiTech Website, there are many useful articles for you to research. Thus, we belive that these blogs are very helpful for your app development. So don’t hesitate. Feel free to visit us at anytime. Hopefully, our blog will help you to develop your iOS app.