Error: value of type "String" does not have a member "Generator" in Swift

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 produces the following error: value of type 'String' has no member 'Generator' in Swift in line: for character in password

I tried to find a possible error on the Internet, but I can’t (plus I'm new to Swift and try to navigate my own languages).

Any help would be greatly appreciated (plus a brief explanation of what I am missing if possible)

+5
source share
1 answer

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 did not iterate over your array in search of a specific character. Changing it to password.characters forces i “look for” each character in the password and voila array. This behavior occurs in swift 2.0 because String no longer conforms to the SequenceType protocol, and String.CharacterView does!

+17
source

All Articles