斯威夫特 - 运营商
Swift 是一种通用的、多范式的编译型编程语言,由 Apple Inc. 开发。在 Swift 中,运算符是告诉编译器在给定表达式中执行操作的符号。就像其他编程语言一样,Swift 中运算符的工作方式也是一样的。我们可以在程序中使用运算符执行多个操作,Swift 中有多个运算符可用。在本文中,我们将学习 Swift 编程语言中存在的所有类型的运算符。
算术运算符
- +(加法):此运算符用于将两个变量相加。
- –(减法):此运算符用于从第一个变量中减去第二个变量。
- /(除法):此运算符用于将第一个变量与第二个变量相除。
- *(乘法):此运算符用于将第一个变量与第二个变量相乘。
- %(模数):此运算符给出第二个除法后的余数。
Swift
// Swift program to illustrate the
// use of arithmetic changes
import Swift
// Creating two variables a and b
var a = 5
var b = 4
// Adding two number
var sum = a + b
print("Addition:", sum)
// Subtracting two number
var sub = a - b
print("Subtraction:", sub)
// Dividing two number
var div = a / b
print("Divide:", div)
// Multiplying two number
var mul = a * b
print("Multiply:", mul)
// Finding Modulus
var mod = a % b
print("Modulus:", mod)
Swift
// Swift program to illustrate the
// use of assignment operators
import Swift
// Creating two variables a & b
var a = 5
var b = 1
// Plus equal to
a += b
print("Result of += operator: ", a)
// Minus equal to
a -= b
print("Result of -= operator: ", a)
// Divide equal to
a /= b
print("Result of /= operator: ", a)
// Multiply equal to
a *= b
print("Result of *= operator: ", a)
// Modulus equal to
a %= b
print("Result of %= operator: ", a)
Swift
// Swift program to illustrate the
// use of bitwise operators
import Swift
// Creating variables a and b
var a = 2
var b = 1
// Left shift operator
print("Bitwise left shift operator: ", a << b)
// Right shift operator
print("Bitwise right shift operator: ", a >> b)
// AND operator operator
print("AND operator: ", a & b)
// OR operator
print("OR operator: ", a | b)
// XOR operator
print("XOR operator: ", a ^ b)
// Tilde operator
print("Tilde operator: ", ~a)
Swift
// Swift program to illustrate the
// use of Comparison operators
import Swift
// Creating two variables a & b
var a = 2
var b = 1
// Less than operator
print(a < b)
// Greater than operator
print(a > b)
// Equal to operator
print(a == b)
// Not equal to operator
print(a != b)
// Less than equal to operator
print(a <= b)
// Greater than equal to operator
print(a >= b)
Swift
// Swift program to illustrate the
// use of Logical Operators
import Swift
// Creating variables a, b & c
var a = 1
var b = 2
var c = 3
// Logical AND operator
if (a < b && b < c)
{
print("Both condition is true")
}
// Logical OR operator
if (a < b || a > c)
{
print("Only one condition is true")
}
// Logical NOT operator
if (true && !false)
{
print("Logic reversed")
}
Swift
// Swift program to illustrate the use of
// Ternary and Nil-coalescing Operator
import Swift
// Creating variables a, b
var a = 1
var b = 2
// Ternary operator
var c = a < b ? 3 : 4
print(c)
let name: String? = nil
// Nil-Coalescing Operator
let newName = name ?? "GFG"
// If we don't pass GFG then the
// default value will be nil in newName
print(newName)
Swift
// Swift program to illustrate the use of
// range Operators
import Swift
// Closed Range Operator - It will print up to 3
for i in 1...3 {
print("closed = \(i)")
}
// Half open Range Operator - It will only print up to 2
for i in 1..<3 {
print("half = \(i)")
}
// One sided Range Operator - It will print all
// values in array from index 0
let values = [ 1, 2, 3, 4 ]
for i in values[0...]
{
print("One sided = \(i)")
}
输出:
Addition: 9
Subtraction: 1
Divide: 1
Multiply: 20
Modulus: 1
赋值运算符
- =(等于):此运算符用于将右侧变量的值分配给左侧变量。
- +=(加等于):该运算符用于将右侧变量添加到左侧变量,并将结果分配给左侧变量。
- -=(减等于):该运算符用于从左变量中减去右变量,并将结果赋给左变量。
- /=(除等于):该运算符用于将左变量与左变量相除,并将结果赋给左变量。
- *=(乘等于):该运算符用于将右侧变量与左侧变量相乘,并将结果赋给左侧变量。
- %=(模数等于):该运算符用于使用两个变量取模并将结果分配给左侧变量
迅速
// Swift program to illustrate the
// use of assignment operators
import Swift
// Creating two variables a & b
var a = 5
var b = 1
// Plus equal to
a += b
print("Result of += operator: ", a)
// Minus equal to
a -= b
print("Result of -= operator: ", a)
// Divide equal to
a /= b
print("Result of /= operator: ", a)
// Multiply equal to
a *= b
print("Result of *= operator: ", a)
// Modulus equal to
a %= b
print("Result of %= operator: ", a)
输出:
6
5
5
5
0
位运算符
- <<(左移):将左变量值左移右变量提到的位数。
- >>(右移):左变量值右移右变量提到的位数。
- & (Binary AND):如果在两个变量中都设置了位,则此运算符将位复制到结果中。
- | (二进制或):如果在任何变量中设置了位,则此运算符将位复制到结果中。
- ^ (Binary XOR):如果位仅在一个变量上设置,则此运算符将位复制到结果中。
- 〜(波浪号):此运算符给出变量的二进制补码
迅速
// Swift program to illustrate the
// use of bitwise operators
import Swift
// Creating variables a and b
var a = 2
var b = 1
// Left shift operator
print("Bitwise left shift operator: ", a << b)
// Right shift operator
print("Bitwise right shift operator: ", a >> b)
// AND operator operator
print("AND operator: ", a & b)
// OR operator
print("OR operator: ", a | b)
// XOR operator
print("XOR operator: ", a ^ b)
// Tilde operator
print("Tilde operator: ", ~a)
输出:
4
1
0
3
3
-3
比较运算符
- <(小于):如果左侧变量的值小于右侧变量的值,则条件为真。
- >(大于):如果左侧变量的值大于右侧变量的值,则条件为真。
- ==(等于):如果两个变量的值相等,则条件为真。
- <=(小于等于):如果左侧变量的值小于或等于右侧变量的值,则条件为真。
- >=(大于等于):如果左侧变量的值大于或等于右侧变量的值,则条件为真。
- !=(不等于):如果两个变量的值不相等,则条件为真。
迅速
// Swift program to illustrate the
// use of Comparison operators
import Swift
// Creating two variables a & b
var a = 2
var b = 1
// Less than operator
print(a < b)
// Greater than operator
print(a > b)
// Equal to operator
print(a == b)
// Not equal to operator
print(a != b)
// Less than equal to operator
print(a <= b)
// Greater than equal to operator
print(a >= b)
输出:
false
true
false
true
false
true
逻辑运算符
- &&(逻辑与):如果两个边条件都为真,则条件为真。
- || (逻辑或):如果任何附带条件为真,则条件为真。
- ! (逻辑非):他的运算符是 反转逻辑状态。
迅速
// Swift program to illustrate the
// use of Logical Operators
import Swift
// Creating variables a, b & c
var a = 1
var b = 2
var c = 3
// Logical AND operator
if (a < b && b < c)
{
print("Both condition is true")
}
// Logical OR operator
if (a < b || a > c)
{
print("Only one condition is true")
}
// Logical NOT operator
if (true && !false)
{
print("Logic reversed")
}
输出:
Both condition is true
Only one condition is true
Logic reversed
杂项运算符
- 条件 ? A : B(三元运算符):如果条件为真,则赋值 A,否则赋值 B。
- (A ?? B) (Nil-coalescing Operator):如果用户没有将任何值传递给变量,那么默认值将是 nil。
- ++(增量):此运算符已用于将变量的值增加 1。
- — (Decrement):此运算符用于将变量的值减 1。
迅速
// Swift program to illustrate the use of
// Ternary and Nil-coalescing Operator
import Swift
// Creating variables a, b
var a = 1
var b = 2
// Ternary operator
var c = a < b ? 3 : 4
print(c)
let name: String? = nil
// Nil-Coalescing Operator
let newName = name ?? "GFG"
// If we don't pass GFG then the
// default value will be nil in newName
print(newName)
输出:
3
GFG
范围运算符
- (a…b) (封闭范围运算符)——它从 a 运行到 b,包括值 a 和 b。它主要用于 for-in 循环。
- (a..半开范围运算符) — 它从 a 运行到 b,但不包括 b 的值。它主要用于 for-in 循环。
- [a…] (单边范围操作员)- 它将运行在一个方向上尽可能远的范围。
迅速
// Swift program to illustrate the use of
// range Operators
import Swift
// Closed Range Operator - It will print up to 3
for i in 1...3 {
print("closed = \(i)")
}
// Half open Range Operator - It will only print up to 2
for i in 1..<3 {
print("half = \(i)")
}
// One sided Range Operator - It will print all
// values in array from index 0
let values = [ 1, 2, 3, 4 ]
for i in values[0...]
{
print("One sided = \(i)")
}
输出:
closed = 1
closed = 2
closed = 3
half = 1
half = 2
One sided = 1
One sided = 2
One sided = 3
One sided = 4
运算符优先级
在 swift 运算符中,优先级用于查找给定表达式中的分组项。此外,用于评估表达式。例如,y = 3 + 4 * 2,这里 * 具有最高优先级,所以首先解决 4 *2,然后将结果与 3 相加。当我们访问下表底部时,优先级会降低。Operator Name Operators Bitwise Shift <<, >> Multiplicative %, *, / Additive |, -, +, -, ^ Range Operators ..<, … Nil-Coalescing Operator ?? Comparison Operators <, >, <=. >=, ==, != Logical Operators &&, || Ternary Operator ? : Assignment Operators |=, %=, /=, *=, >>=, <<=, ^=, +=, -=