Swift operators: A brief overview for iOS developers

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:

Swift operators

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:

Swift operators

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:

OperatorOperationFor example
+Addition2+3=5
Subtraction5-2=3
*Multiplication2*3=6
/Division6/3=2
%Modulo5%2=1

NOTE: Only integers are used with the Modulo operator.

For example:

Input

var a = 7

var b = 2

// addition

print (a + b)  

// subtraction

print (a – b) 

// multiplication

print (a * b)  

Output

Swift

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:

OperatorNameFor example
=Assignment Operatora = 7
+=Addition Assignmenta += 1 // a = a + 1
-=Subtraction Assignmenta -= 3 // a = a – 3
*=Multiplication Assignmenta *= 4 // a = a * 4
/=Division Assignmenta /= 3 // a = a / 3
%=Remainder Assignmenta %= 10 // a = a % 5

For example:

Input

// 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

print(a)

Output

Comparison Swift Operators

When two values/variables are compared, a boolean result is returned: true or false. For example:

Swift operators

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:

OperatorMeaningFor example
==Is Equal To4 == 6 gives us false
!=Not Equal To4 != 6 gives us true
>Greater Than4 > 6 gives us false
<Less Than4 < 6 gives us true
>=Greater Than or Equal To4 >= 6 give us false
<=Less Than or Equal To4 <= 6 gives us true

NOTE: In decision-making and loops, comparison operators are utilized. 

For example:

Input

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)

Output

iOS app development

Logical Operators

To determine whether an expression is true or false, we should use Logical Swift Operators. Because they are utilized to make decisions. For example:

logical operators

As you can see, the logical operator AND is used here. Therefore, the result is true because both a > 2 and b >= 6.

OperatorFor exampleMeaning
&&x && yLogical AND: true only if both the operands are true
||x || yLogical OR: true if at least one of the operands is true
!!xLogical NOT: true if the operand is false and vice-versa.

For example:

Input

// logical AND

print(true && true)      // true

print(true && false)     // false

// logical OR

print(true || false)      // true

// logical NOT

print(!true)                  // false

Output

operators logical

Bitwise Operators

Firstly, Bitwise Swift Operators are used to do operations on single bits.

OperatorDescription
&Binary AND
|Binary OR
^Binary XOR
~Binary One’s Complement
<<Binary Shift Left
>>Binary Shift Right
pqp&qp|qp^q
00000
01011
11110
10011

For example:

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

Range Operators

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.

OperatorDescriptionFor example
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 Rangea… : 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:

OperatorDescriptionFor example
? :Ternary Operator – returns value based on the condition(5 > 2) ? “Success” : “Error” // Success
??Nil-Coalescing Operator – checks whether an optional contains a value or notnumber ?? 5
Range Operator – defines range containing values1…3 // range containing values 1,2,3

Further reading

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:

Firstly, The useful explanation you need to know about Swift init

Secondly, Swift alert: The effective way to show it with UIAlertController

Thirdly, What is the Swift function and how does it work?

Fourthly, Definition, Benefits, Types and practical examples of Swift protocol

Finally, Swift computed property: what is it, how it work and when should you use it

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. 

Tags

Share