David Peterson
David Peterson

Reputation: 19

Swift: How do I cast a Character as a String to do a simple compare?

I am getting the error at the "==" in code Cannot invoke '==' with an argument list of type '(@lvalue String, @lvalue Character)'

--------representation of my code

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")
    }
}

Upvotes: 0

Views: 279

Answers (1)

Alex
Alex

Reputation: 5119

One very simple way is to use string interpolation:

if guessLetter == "\(oneLetter)"

Another is to have guessLetter be a Character:

var guessLetter: Character = "g"

Upvotes: 2

Related Questions