I use swift and what i want to do is to check this:
if string.characterAtIndex(i) == "a"
But i get error. How to convert this "a" so that can be same type with characters i loop.
Thanks.
I use swift and what i want to do is to check this:
if string.characterAtIndex(i) == "a"
But i get error. How to convert this "a" so that can be same type with characters i loop.
Thanks.
You need to convert you UniChar
- characterAtIndex(i)
to a Character
, so you can compare them.
Solution:
let ithChar:Character = Character(UnicodeScalar(string.characterAtIndex(i)))
if ithCahr == "a"
{
//do some stuff
}
Hope it helps!
Here is another way you could do it:
if string.characterAtIndex(i) == "a".characterAtIndex(0)
In better explanatory way, this can be one of the ways to perform what you intend to do.
var str = "Hello, playground"
var newString = str as NSString
var num:Int = countElements(str)
for var i = 0; i < num; i++
{
if ( Array(str)[i] == "p") {
println(Array(str)[i]) // will show the output
println("success");
break;
}
}