Character Comparison in Swift

let a:Character = "a"  // Would not really be a literal in my app...
let b:Character = "b"  // ...but this illustrates the issue
let compare = a == b 

The compiler complains: it was not possible to find an overload for ==, which takes the provided arguments.

This is despite the fact that if you right-click on Character, you can easily find this ad.

func ==(lhs: Character, rhs: Character) -> Bool

Any suggestions? I can get around by assigning characters to strings and doing string comparisons, but I repeat thousands of characters. Of course, there is a quick way.

+4
source share
2 answers

That should work. Your code is displayed here.

Code above shown in an Xcode playground

+4
source

This works without a problem:

  3> let a: Character = "a"
a: Character = SmallRepresentation {
  SmallRepresentation = 9223372036854775649
}
  4> let b: Character = "b"
b: Character = SmallRepresentation {
  SmallRepresentation = 9223372036854775650
}
  5> let compare = (a == b)
compare: (Bool) = false
  6> let compare2 = a == b
compare2: Bool = false
0
source

Source: https://habr.com/ru/post/1543491/


All Articles