Swift – Operators

Swift – Operators ”; Previous Next What is an Operator in Swift? An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Or we can say that operators are special symbols that are used to perform specific operations between one or more operands. For example, to add two numbers such as 34 and 21 we will use the + operator (34 + 21 = 55). Type of Operators in Swift Swift supports the following types of operators − Arithmetic Operators Comparison Operators Logical Operators Bitwise Operators Assignment Operators Range Operators Misc Operators Let”s discuss each operator separately in detail. Swift Arithmetic Operators Arithmetic operators are used to perform mathematic operations like addition, division, multiplication, etc. on the given operands. They always work on two operands but the type of these operands should be the same. Swift supports the following arithmetic operators − Operator Name Example + Addition 20 + 30 = 50 – Subtraction 30 – 10 = 20 * Multiplication 10 * 10 = 100 / Division 20 / 5 = 4 % Modulus 10 % 2 = 0 Swift Comparison Operators Comparison operators are used to compare two operands and find the relationship between them. They return the result into a boolean(either true or false). Swift supports the following comparison operators − Operator Name Example (==) Equal 10 == 10 = true != Not Equal 34 != 30 = true > Greater than 90 > 34 = true < Less than 12 < 34 = true >= Greater than or Equal to 30 >= 10 = true <= Less than or Equal to 10 <= 32 = true Swift Logical Operators Logical operators are used to perform logical operations between the given expressions. It can also make decisions on multiple conditions. It generally works with Boolean values. Swift supports the following logical operators − Operator Name Example && Logical AND X && Y || Logical OR X || Y ! Logical NOT !X Swift Bitwise Operators Bitwise operators are used to manipulate individual bits of the integer. They are commonly used to perform bit-level operations. Swift supports the following bitwise operators − Operator Name Example & Bitwise AND X & Y | Bitwise OR X | Y ^ Bitwise XOR X ^ Y ~ Bitwise NOT ~X << Left Shift X << Y >> Right Shift X >> Y Swift Assignment Operators Assignment operators are used to assign and update the value of the given variable with the new value. Swift supports the following assignment operators − Operator Name Example (=) Assignment X = 10 += Assignment Add X = X + 12 -= Assignment Subtract X = X – 12 *= Assignment Multiply X = X * 12 /= Assignment Divide X = X / 12 %= Assignment Modulus X = X % 12 <<= Assignment Left Shift X = X << 12 >>= Assignment Right Shift X = X >> 12 &= Bitwise AND Assignment X = X & 12 ^= Bitwise Exclusive OR Assignment X = X ^12 |= Bitwise Inclusive OR Assignment X = X | 12 Swift Misc Operators Apart from the general operators Swift also supports some special operators and they are − Operator Name Example – Unary Minus -9 + Unary Plus 2 Condition ? X : Y Ternary Conditional If Condition is true ? Then value X : Otherwise value Y Swift Advance Operators Apart from the basic operators Swift also provides some advanced operators that are used to manipulate complex values and they are − Arithmetic Overflow Operators Identity Operators Identity Operators Let”s discuss each operator separately in detail. Swift Arithmetic Overflow Operators Arithmetic overflow operators are used to perform arithmetic operations and handle overflow very well if occurs. Or we can say that arithmetic operators work with those integers whose value may exceed the maximum or minimum bound. Swift supports the following arithmetic overflow operators − Operator Name Example &+ Overflow Addition Num1 &+ Num2 &- Overflow Subtraction Num1 &- Num2 &* Overflow Multiplication Num1 &* Num2 Swift Identity Operators Identity operators are used to determine whether the given variable refers to the same instance or not. These operators work with objects and classes. They are referenced type operators.

Swift – Logical Operators

Swift – Logical Operators ”; Previous Next Logical Operator in Swift Logical operators are used to perform logical operations on multiple expressions together. They always return boolean values either true or false. They are commonly used with conditional statements and loops to return decisions according to the boolean conditions. You can also combine them to manipulate boolean values while dealing with complex expressions. Swift supports the following logical operators − Operator Name Example && AND A && B = false || OR A || B = true ! NOT !(A && B) = true AND Operator in Swift The AND operator returns true if both the given expressions are true. If any one of the expressions is false, then this operator will return false. It is an infix operator means this operator only works when you place it in between two operands. Syntax Following is the syntax of the AND operator − var result = expression1 && expression2 Example Swift program to perform logical AND operation between two expressions using AND operator (&&). import Foundation let age = 22 let height = 185 if (age > 18) && (height > 182){ print(“You are eligible for the Police exam”) } else { print(“You are not eligible”) } Output You are eligible for the Police exam OR Operator in Swift The OR operator returns true if only one of the expressions among the two given expressions is true. If both the expressions are false, then this operator will return false. It is an infix operator means it will be placed between two operands to perform its action. Syntax Following is the syntax of the OR operator − var result = expression1 || expression2 Example Swift program to perform logical OR operation between two expressions using OR operator (||). import Foundation let marks = 640 let experience = 3 if (marks > 500) || (experience > 4){ print(“You are eligible for the PO Post”) } else { print(“You are not eligible”) } Output You are eligible for the PO Post NOT Operator in Swift A NOT operator is used to invert the boolean value. If the value is true, then it will convert into false. Whereas if the value is false then it will convert into true. It is a prefix operator and is placed just before the expression whose value you want to invert. Syntax Following is the syntax of the NOT operator − !(expression1 && expression2) Example Swift program to perform logical NOT operation on the expressions using NOT operator (!). import Foundation let x = 20 let y = 40 // Here expression gives a true result but NOT operator converts it into a false if !(x > 0 && y > 0) { print(“Given values are greater than 0”) } else { print(“Given values are less than 0”) } Output Given values are less than 0 Combining Logical Operators in Swift In Swift, we can also combine multiple logical operators in a single expression. It will create a long compound expression. As we know AND and OR are left-associative so in the compound expression the left-side expression will evaluate first. Example Swift program to combine multiple logical operators. import Foundation let password = “vsv@v3″ let username = “mona” let mainPassword = “MO@12s” if (password == “XP123” || username == “mona”) && mainPassword == “MO@12s”{ print(“Welcome to the digital locker”) } else { print(“Error!!! Please enter correct detail”) } Output Welcome to the digital locker swift_operators.htm Print Page Previous Next Advertisements ”;

Swift – Overview

Swift Overview ”; Previous Next Introduction to Swift Programming Language Swift is a new generation programming language developed by Apple Inc. for iOS and OS X development. Swift adopts the best of C and Objective-C, without the constraints of C compatibility. Due to modern syntax, safety features, and optimized performance Swift is fast, concise, expressive and easy to use. It makes the development process easier by providing features like automatic memory management, type inference, etc. Using Swift we can create dynamic applications for mobile, desktops or even for server-side applications. Its main aim is to provide a seamless and robust development experience to its users. History of Swift The history of Swift began in 2010 when an Apple engineer named Chris Lattner started working on a new programming language. His main aim was to craft a programming language that is easy to use and more expressive just like high-level languages and provides great performance like low-level languages. He achieves his goal by creating Swift programming language. So in 2014, Apple unveiled Swift to the world for the very first time at WWDC (World Wide Developer Conference) to replace Objective-C for iOS and macOS development. After that, the evolution process went on and created the following versions of Swift with updates and improvements. The latest version of Swift is 5.9. Version Release Year Swift 1.0 2014 Swift 1.2, Swift 2.0 2015 Swift 3.0 2016 Swift 4.0 2017 Swift 4.1, 4.2 2018 Swift 5.0, Swift 5.1 2019 Swift 5.3 2020 Swift 5.4, Swift 5.5 2021 Swift 5.6, Swift 5.7 2022 Swift 5.8, Swift 5.9 2023 Characteristics of Swift Swift offers various characters to its developers which will help them to develop optimized and dynamic applications for the Apple platform and the key characteristics are − Modern Syntax − Swift provides clean and expressive syntaxes so that the developer can develop concise and easy-to-read programs. It makes it a more approachable language for both beginner and experienced developers. Safety − It increases safety by removing common bugs and errors. It incorporates modern programming techniques which makes it more secure and reliable. Performance − It provides high performance just like low-level languages while maintaining the safety and expressiveness of the code. Interoperability − Swift is an interoperable language. It works seamlessly with other languages like Objective-C. Developers are allowed to use Swift and Objective-C code together in the same project. Open Source − Swift is an open-source programming language that enhances the collaboration, innovation and ongoing improvements of Swift. Optionals − Swift provides great support to the optionals so that developers can explicitly represent the absence of value. It handles null or undefined values very well without the risk of runtime crash. Type inference − Swift supports a strong type system but it also incorporates with type interface to reduce the requirement of explicit type annotation. This means the Swift compiler is capable of analyzing the variables and expressions and determining their type. Automatic Memory Management − Swift uses Automatic Reference Counting to manage memory. It handles memory allocation and deallocation automatically without any lag. Because allocating memory manually is the common source of errors for the developers. Closures − Swift provides great support to the closures. Closure is a block code that can be referenced, passed around and executed later. They enhance the modularity and flexibility of the program. Protocol-Oriented Programming − Swift encourages protocol-oriented programming. It emphasizes the use of protocols to create blueprints for functionality. It creates reusable, modular and composable codes. Application of Swift Swift is primarily used for developing applications for Apple’s platform but it can also be used in developing applications for other platforms. Although Swift has numerous application which is impossible to note down, so we provide some of the major applications of Swift − iOS App Development − It is the most preferred language for developing applications for iOS devices like iPad, iPhone, etc. macOS Development − It is also used for creating applications, utilities, and software for the macOS operating system. watchOS App development − Using Swift we can also create a wide range of health applications for Apple Watch. tvOS App Development − With the help of Swift we can also create various entertainment apps for tvOS. Cross-Platform Development − Swift is not limited to Apple”s platform, we can also create applications for other platforms using cross-platform development. For example, the SwiftUI framework is used to create applications or interfaces that run on both Windows and iOS. Server-Side Development − Swift is also used in server-side development. It allows developers to develop web applications, APIs, and services using server-side Swift frameworks such as Vapour, Kitura and Perfect. Disadvantages of Swift Every programming language has its pros and cons. Similarly, Swift also has various advantages and disadvantages. So, some of the major disadvantages of Swift are − Swift is primarily developed for creating applications for Apple’s devices. So outside Apple’s ecosystem, it has limited use because developers have many alternative options for the cross-platform language. Swift is a new programming language so it does not have mature tools like other programming languages. As compared to Java, JavaScript or Python it has a small developer pool. The compatibility with non-Apple platforms is limited. Swift supports type inference and optimization, so if the project is larger then the compilation time is long. Print Page Previous Next Advertisements