Mastering Swift
上QQ阅读APP看书,第一时间看更新

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 > varB
Less than:  varA < varB
Greater than or equal to:  varA >= varB
Less than or equal to:  varA <= varB

Example:

2 == 1 //false, 2 does not equal 1
2 != 1 //true, 2 does not equal 1
2 > 1  //true, 2 is greater than 1
2 < 1  //false, 2 is not less than 1
2 >= 1 //true, 2 is greater or equal to 1
2 <= 1 //false, 2 is not less or equal to 1

Arithmetic operators

Description: The arithmetic operators perform the four basic mathematical operations.

Prototypes:

Addition:  varA + varB
Subtraction:  varA – varB
Multiplication:  varA * varB
Division:  varA / varB

Example:

var x = 4 + 2  //x will equal 6
var x = 4 – 2  //x will equal 2
var x = 4 * 2  //x will equal 8
var x = 4 / 2  //x will equal 2
var x = "Hello " + "world"  //x will equal "Hello World"

The remainder operator

Description: The remainder operator calculates the remainder if the first operand is pided by the second operand.

Prototype:

varA % varB

Example:

var x = 10 % 3  //x will equal 1
var x = 10 % 2.6  //x will equal 2.2

Increment and decrement operators

Description: The increment and decrement operators are shortcuts to increment or decrement a variable by 1.

Prototypes:

++varA - Increments the value of varA and then returns the value

varA++ - Returns the values of varA and then increments varA
--varA – Decrements the value of varA and then returns the value
varA-- - Returns the value of varA and then decrements varA

Example:

var x = 5
var y = ++x  //Both x and y equals 6
var y = x++  //x equals 6 but y equals 5
var y = --x  //Both x and y equals 4
var y = x--  //x equals 4 but y equals 5

Compound assignment operators

Description: The compound assignment operators combine an arithmetic operator with an assignment operator.

Prototypes:

varA += varB
varA -= varB
varA *= varB
varA /= varB

Example:

var x = 6
x += 2  //x is equal to 8
x -= 2  //x is equal to 4
x *= 2  //x is equal to 12
x /= 2  //x is equal to 3

The ternary conditional operator

Description: The ternary operator assigns a value to a variable based on the evaluation of a comparison operator or Boolean value.

Prototype:

(boolValue ? valueA : valueB)

Example:

var x = 2
var y = 3
var z = (y > x ? "Y is greater" : "X is greater")  //z equals "Y is greater"

The logical NOT operator

Description: The logical NOT operator inverts a Boolean value.

Prototype:

varA = !varB

Example:

var x = true
var y = !x  //y equals false

The logical AND operator

Description: The logical AND operator returns true if both operands are true, otherwise it returns false.

Prototype:

varA && varB

Example:

var x = true
var y = false
var z = x && y  //z equals false

The logical OR operator

Description: The logical OR operator returns true if either of the operands is true.

Prototype:

varA || varB

Example:

var x = true
var y = false
var z = x || y  //z equals true

For those that are familiar with C and languages that have a similar syntax to C, these operators should look pretty familiar. For those that aren't that familiar with the C operators, rest assured that you will use them enough and they will become second nature.