Comparison Operators
Comparison operators work with boolean and integer values.
Equality ==
-
Equality:
==
is supported for booleans, numbers, addresses, strings, characters, enums, paths,Type
values, references, andVoid
values (()
). Variable-sized arrays, fixed-size arrays, dictionaries, and optionals also support equality tests if their inner types do.Both sides of the equality operator may be optional, even of different levels; for example, it is possible to compare a non-optional with a double-optional (
??
):_101 == 1 // is `true`_10_101 == 2 // is `false`_10true == true // is `true`_10_10true == false // is `false`_10let x: Int? = 1_10x == nil // is `false`_10let x: Int = 1_10x == nil // is `false`_10// Comparisons of different levels of optionals are possible._10let x: Int? = 2_10let y: Int?? = nil_10x == y // is `false`_10// Comparisons of different levels of optionals are possible._10let x: Int? = 2_10let y: Int?? = 2_10x == y // is `true`_10// Equality tests of arrays are possible if their inner types are equatable._10let xs: [Int] = [1, 2, 3]_10let ys: [Int] = [1, 2, 3]_10xs == ys // is `true`_10_10let xss: [[Int]] = [xs, xs, xs]_10let yss: [[Int]] = [ys, ys, ys]_10xss == yss // is `true`_10// Equality also applies to fixed-size arrays. If their lengths differ, the result is a type error._10let xs: [Int; 2] = [1, 2]_10let ys: [Int; 2] = [0 + 1, 1 + 1]_10xs == ys // is `true`_10// Equality tests of dictionaries are possible if the key and value types are equatable._10let d1 = {"abc": 1, "def": 2}_10let d2 = {"abc": 1, "def": 2}_10d1 == d2 // is `true`_10_10let d3 = {"abc": {1: {"a": 1000}, 2: {"b": 2000}}, "def": {4: {"c": 1000}, 5: {"d": 2000}}}_10let d4 = {"abc": {1: {"a": 1000}, 2: {"b": 2000}}, "def": {4: {"c": 1000}, 5: {"d": 2000}}}_10d3 == d4 // is `true`
Inequality !=
-
Inequality:
!=
is supported for booleans, numbers, addresses, strings, characters, enums, paths,Type
values, references, andVoid
values (()
). Variable-sized arrays, fixed-size arrays, dictionaries, and optionals also support inequality tests if their inner types do.Both sides of the inequality operator may be optional, even of different levels; for example, it is possible to compare a non-optional with a double-optional (
??
):_101 != 1 // is `false`_10_101 != 2 // is `true`_10true != true // is `false`_10_10true != false // is `true`_10let x: Int? = 1_10x != nil // is `true`_10let x: Int = 1_10x != nil // is `true`_10// Comparisons of different levels of optionals are possible._10let x: Int? = 2_10let y: Int?? = nil_10x != y // is `true`_10// Comparisons of different levels of optionals are possible._10let x: Int? = 2_10let y: Int?? = 2_10x != y // is `false`_10// Inequality tests of arrays are possible if their inner types are equatable._10let xs: [Int] = [1, 2, 3]_10let ys: [Int] = [4, 5, 6]_10xs != ys // is `true`_10// Inequality also applies to fixed-size arrays. If their lengths differ, the result is a type error._10let xs: [Int; 2] = [1, 2]_10let ys: [Int; 2] = [1, 2]_10xs != ys // is `false`_10// Inequality tests of dictionaries are possible if the key and value types are equatable._10let d1 = {"abc": 1, "def": 2}_10let d2 = {"abc": 1, "def": 500}_10d1 != d2 // is `true`_10_10let d3 = {"abc": {1: {"a": 1000}, 2: {"b": 2000}}, "def": {4: {"c": 1000}, 5: {"d": 2000}}}_10let d4 = {"abc": {1: {"a": 1000}, 2: {"b": 2000}}, "def": {4: {"c": 1000}, 5: {"d": 2000}}}_10d3 != d4 // is `false`
Less than <
-
Less than:
<
is supported for integers, booleans, characters, and strings:_231 < 1 // is `false`_23_231 < 2 // is `true`_23_232 < 1 // is `false`_23_23false < true // is `true`_23_23true < true // is `false`_23_23"a" < "b" // is `true`_23_23"z" < "a" // is `false`_23_23"a" < "A" // is `false`_23_23"" < "" // is `false`_23_23"" < "a" // is `true`_23_23"az" < "b" // is `true`_23_23"xAB" < "Xab" // is `false`
Less or equal than <=
-
Less or equal than:
<=
is supported for integers, booleans, characters, and strings:_251 <= 1 // is `true`_25_251 <= 2 // is `true`_25_252 <= 1 // is `false`_25_25false <= true // is `true`_25_25true <= true // is `true`_25_25true <= false // is `false`_25_25"c" <= "a" // is `false`_25_25"z" <= "z" // is `true`_25_25"a" <= "A" // is `false`_25_25"" <= "" // is `true`_25_25"" <= "a" // is `true`_25_25"az" <= "b" // is `true`_25_25"xAB" <= "Xab" // is `false`
Greater than >
-
Greater than:
>
is supported for integers, booleans, characters, and strings:_251 > 1 // is `false`_25_251 > 2 // is `false`_25_252 > 1 // is `true`_25_25false > true // is `false`_25_25true > true // is `false`_25_25true > false // is `true`_25_25"c" > "a" // is `true`_25_25"g" > "g" // is `false`_25_25"a" > "A" // is `true`_25_25"" > "" // is `false`_25_25"" > "a" // is `false`_25_25"az" > "b" // is `false`_25_25"xAB" > "Xab" // is `true`
Greater or equal than >=
-
Greater or equal than:
>=
is supported for integers, booleans, characters, and strings:_251 >= 1 // is `true`_25_251 >= 2 // is `false`_25_252 >= 1 // is `true`_25_25false >= true // is `false`_25_25true >= true // is `true`_25_25true >= false // is `true`_25_25"c" >= "a" // is `true`_25_25"q" >= "q" // is `true`_25_25"a" >= "A" // is `true`_25_25"" >= "" // is `true`_25_25"" >= "a" // is `true`_25_25"az" >= "b" // is `true`_25_25"xAB" >= "Xab" // is `false`
Comparing number super-types
Similar to arithmetic operators, comparison operators are also not supported for number supertypes (Number
, SignedNumber
FixedPoint
, SignedFixedPoint
, Integer
, and SignedInteger
), as they may or may not succeed at run-time:
_10let x: Integer = 3 as Int8_10let y: Integer = 4 as Int8_10_10let z: Bool = x > y // Static error
Values of these types must be cast to the desired type before performing the arithmetic operation:
_10let z: Bool = (x as! Int8) > (y as! Int8)