比较Swift中的字符

时间:2014-06-06 00:47:28

标签: compare character 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 

编译器抱怨:找不到接受提供的参数的==的重载。

尽管如果您右键单击Character,您可以轻松找到此声明

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

有什么建议吗?我可以通过将字符分配给字符串并进行字符串比较来解决,但我正在迭代数千个字符。当然有一种迅捷的方式。

2 个答案:

答案 0 :(得分:4)

这实际上应该有效。这是您提供的代码的输出。

Code above shown in an Xcode playground

答案 1 :(得分:0)

这没有问题:

  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