Book Image

Mastering Swift

By : Jon Hoffman
Book Image

Mastering Swift

By: Jon Hoffman

Overview of this book

Table of Contents (22 chapters)
Mastering Swift
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Operators


An operator is a symbol or combination of symbols that we can use to check, change, or combine values. We have used operators in most of the examples in the first two chapters; however, we did not specifically call them operators. In this section, we will show how to use most of the basic operators that Swift supports.

Swift supports most standard C operators and also improves them to eliminate several common coding errors. For example, the assignment operator does not return a value to prevent it from being used when the equality operator (==) was meant to be used.

Let's look at the operators in Swift.

The assignment operator

Description: The assignment operator initializes or updates a variable.

Prototype:

varA = varB

Example:

let x = 1
var y = "Hello"
a = b

Comparison operators

Description: The comparison operator returns a Boolean true if the statement is true or a Boolean false if the statement is not true.

Prototypes:

Equality:  varA == varB
Not equal:  varA != varB
Greater than:  varA...