Andrew Duncan
Andrew Duncan

Reputation: 3753

Comparing Characters 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: Could not find an overload for == that accepts the supplied arguments.

This despite the fact that if you right-click on Character you can easily find this declaration

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

Any suggestions? I can work around by assigning the Characters to Strings and doing a String compare, BUT I am iterating over thousands of Characters. Surely there is a Swift Way.

Upvotes: 4

Views: 16423

Answers (2)

GoZoner
GoZoner

Reputation: 70165

This works w/o 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

Upvotes: 0

Brendan G. Lim
Brendan G. Lim

Reputation: 391

This should actually work. Here's the output of your supplied code.

Code above shown in an Xcode playground

Upvotes: 6

Related Questions