I have this piece of code in Swift:
var password = "Meet me in St. Louis"
for character in password {
if character == "e" {
print("found an e!")
} else {
}
}
which throws the following error: value of type 'String' has no member 'Generator' in Swift
in line: for character in password
I have tried to find online the possible error, but I can't (plus I am new to Swift and trying to navigate myself through the idiosyncrasies of the language).
Any help would be much appreciated (plus a brief explanation of what I am missing if possible)
In order for your code to work you need to do this :
var password = "Meet me in St. Louis"
for character in password.characters {
if character == "e" {
print("found an e!")
} else {
}
}
The problem with your code not working was the fact that it was not iterating over your array looking for a particular Character. Changing it to password.characters
forces i
to "search" each Character of the array password
and voila.This behaviour happens in swift 2.0 because String
no longer conforms to SequenceType
protocol while String.CharacterView
does!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With