Operator : An operator is a special symbol or phrase that you use to check, change, or combine values.Swift also provides two range operators (a..<b and a...b) not found in C.
The remainder operator (a % b)
Compound Assignment Operators
Swift provides compound assignment operators that combine assignment (=) with another operation. Example
Comparison Operators:
let simulatorToken = "werjf123123kjsdjf"
var deviceToken: String?
let setDeviceToken = deviceToken ?? simulatorDeviceToken
let names = ["Macbook-proretina", "Macmini", "iMac", "Macbook-pro"]
Types of Operator in Swift
- Unary
- Binary
- Ternary
Unary: Unary operators operate on a single target. Unary prefix operators appear immediately before their target and unary postfix operators appear immediately after their target . Example
Unary prefix operator: +a
Unary postfix operator: a+
Binary: Binary operators operate on two targets (such as a + b) and are infix because they appear in between their two targets.
Ternary: Ternary operators operate on three targets. Swift has only one ternary operator, the ternary conditional operator (a ? b : c)
let x = 10
let y = 20
print("Largest number is \(x > y ? x : y)")
let y = 20
print("Largest number is \(x > y ? x : y)")
Output : 20
Assigment Operator: The assignment operator (a = b) initializes the value of a with the value of b. Example
a = b
Arithmetic Operators
Swift supports the four standard arithmetic operators for all number types:
Arithmetic Operators
Swift supports the four standard arithmetic operators for all number types:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
10 % 3 = 1
Compound Assignment Operators
Swift provides compound assignment operators that combine assignment (=) with another operation. Example
var x = 10
x += 2
x += 2
// x is now 12
*Note: The compound assignment operators do not return a value.
let z = x +=2
Comparison Operators:
- Equal to (a == b)
- Not equal to (a != b)
- Greater than (a > b)
- Less than (a < b)
- Greater than or equal to (a >= b)
- Less than or equal to (a <= b)
Identity Operator in Swift : Identity operator help us to test whether two object references both refer to the same object instance.
- ===
- !==
var deviceToken: String?
let setDeviceToken = deviceToken ?? simulatorDeviceToken
Output = werjf123123kjsdjf
Range Operators:
Range Operators:
- Closed Range Operator (a...b)
- Half - Open Range Operator (a..<b)
Closed Range Operator: The closed range operator (a...b) defines a range that runs from a to b, and includes the values a and b. The value of a must not be greater than b. Example
for i in (1...10){
print (i)
}
Half - Open Range Operator: The half-open range operator (a..<b) defines a range that runs from a to b, but does not include b.
let names = ["Macbook-proretina", "Macmini", "iMac", "Macbook-pro"]
for index in 0 ..< names.count {
print (index)
}
Logical Operators:combine the Boolean logic values true and false.
Logical Operators:combine the Boolean logic values true and false.
- Logical NOT (!a)
- Logical AND (a && b)
- Logical OR (a || b)
No comments:
Post a Comment