Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: value of type 'String' has no member 'Generator' in Swift [duplicate]

Tags:

swift

swift2

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)

like image 486
TheoK Avatar asked Sep 10 '15 21:09

TheoK


1 Answers

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!

like image 88
Korpel Avatar answered Oct 21 '22 05:10

Korpel