Swift:如何将字符转换为字符串来进行简单的比较?

时间:2015-01-28 19:52:04

标签: xcode string swift compare character

我在代码中的“==”处收到错误  无法使用类型为'(@lvalue String,@lvalue Character)'的参数列表调用'=='

--------代表我的代码

var randomWord = "horse"
var guessLetter = "g"

for var charIndex = 0; charIndex < countElements(randomWord); charIndex++ {
    var index = advance(str.startIndex, charIndex)
    var oneLetter = str[index]

    if guessLetter == oneLetter {
        println("ok")
    } else {
        println("nope")
    }
}

1 个答案:

答案 0 :(得分:2)

一种非常简单的方法是使用string interpolation

if guessLetter == "\(oneLetter)"

另一个是让guessLetter成为一个角色:

var guessLetter: Character = "g"