Swift operators

As we all know operators are the basic and fundamental concepts in any programming languages. In Swift, operators are same as other programming language operators, but Swift operators are more efficient and simplified than other languages which help developers using it efficiently. let’s go through operators, then on the go lets see the swift specific operators. Is it boring? if so, please bear with me.

For what we need operators and what is the purpose of it?. Simple, manipulating the values and checking the multiple variables and decide the flow of the code using decision-making syntax.

Operators are Unary, binary and ternary.

Unary Operator:

Unary operators work on a variable. Explaining by words will rotate our brain in 360 degrees. right.. let’s see an example for unary operators
lets say boolean variable ‘a’ is there, and we want to change the state of it, like true to false or vice versa.
a = !a
So here, ‘!’  is an unary operator, which operands on a variable named ‘a’.
a = -a
where, ‘-‘ operator changes it to a negative value if it is positive.

Hope that at this moment you could get the idea of binary operator

Binary Operator:

Nothing to explain in detail, as unary operator works on a single variable, a binary operator has two operands, wanna put one technical word, that’s why operands. So binary works on two variables like below
Addition -> a = a + b
Subtraction -> a = a - b
Multiplication -> a = a * b
Division -> a = a / b
Modulo -> a = a % b
Hope that, you can easily point the binary operators on the sample above, yes +,-,/,*% are the binary operators.

Ternary Operators:

Now hope that I need not explain about ternary operators, if I say again, like if an operator works on three operands then it is a ternary operator. You will throw a stone on me. But the sad part I have to say the same. please pick the small one. let’s see an example for that.

‘? :’ is a ternary operator, let’s take an example, we need to check the boolean, we need to print as true if it is true, or false if it is false.
how we will do generally using if condition.
Bool b = false
if b == true {
print("yes, it is true")
print("no,it is false")
We have written six lines for that, how it would be if we use ternary operator and write the above case in a line. Simply superb right!!
(b == false) ? print(“yes,it is false”) : print(“no,it is false”)

Assignment Operator:

The assignment operator is to initialize or update the value of a variable.
below is an example for that let b = 10
As I said in the beginning of this tutorial, I am pointing the difference of assignment operator between C, objective C, and Swift.

in objective C
if a = b {
} // is valid, since it will return a value after assigning the value of b to a, but in Swift, the same condition is not valid since assignment operator in Swift never returns a value.

Arithmetic Operators:

Swift supports the four standard arithmetic operators
addition +
subtraction –
multiplication *
division /

Another significant thing in swift, I like to highlight here is, addition + operator also supports for String concatenation. we can see String in detail in next chapter.

Comparison Operators:

Like other programming languages, Swift supports following standard Comparison operators.

equal to (a == b )
not equal to (a != b )
less than (a < b)

greater than ( a > b)
less than or equal to ( a b)
greater than or equal to ( a b )

Identity operator:

Swift also provides identity operators which help to test whether two object references both refer to the same object instance.

Range operators:

Range operators are expressing of the range of values. Swift provides two type of range operators.

Closed Range operator
Half-Open Range operator.

Closed Range operator:

The syntax for closed range operators is a…b, it includes both a and b

for i in 1...5{
print(" i = \(i)")

Half Open Range Operator:

The half-open range operator( a..<b ) also defines a range that includes a but not includes b.

for i in 1..<5{
print(" i = \(i)")

the above example does not print the 5th value, as it does not include 5 in the range.

Logical operator:

Swift supports standard logical operators,
|| or operator
&& and operator

Nil-Coalescing Operator:

?? is nil coalescing operator, so what it does, it unwraps the variable to know the value and use it. If it does not have value, it sets nil. I can read your mind voice that expecting an example.

Lets assume

var a : String?

let b = a ?? “a is nil”

from the above example, our Nil-Coalescing operator checks the value of string a by unwrapping it and assign that value to b, if a is nil while unwrapping, it sets string as “a is nil” to b.

This is nice right, we read ternary operator to make use of if condition efficiently, now we are having a Nil-Coalescing operator which is more elegant way to encapsulate this conditional checking.



16 Replies to “Swift operators”

  1. Hello there I am so delighted I found your website, I really found you
    by accident, while I was researching on Bing for something
    else, Anyhow I am here now and would just like to say thanks a lot for a fantastic post and a all round
    interesting blog (I also love the theme/design), I don’t
    have time to read through it all at the moment but I have saved it and also added in your RSS feeds,
    so when I have time I will be back to read a great deal more, Please do keep up the excellent job.

    1. Thanks man..love to hear this, and your words put me in energetic mode to continue writing.. pls do visit regularly , since i have plan to give complete tutorial

  2. Hmm it seems like your website ate my first comment (it was super long) so I guess I’ll just sum it up what I had written and
    say, I’m thoroughly enjoying your blog. I as well am an aspiring blog blogger but
    I’m still new to the whole thing. Do you have any recommendations for beginner
    blog writers? I’d definitely appreciate it.

  3. After I initially commented I seem to have clicked the -Notify me when new comments are added-
    checkbox and now whenever a comment is added I recieve 4 emails with
    the exact same comment. Is there an easy method you can remove me from that service?
    Many thanks!

  4. I was very happy to uncover this site. I wanted to thank
    you for your time due to this wonderful read!! I definitely liked every little bit of it
    and i also have you book marked to check out new stuff on your web site.

Leave a Reply

Your email address will not be published. Required fields are marked *