jweinst1
9/26/2016 - 7:40 PM

iterator for chars in a string in swift

iterator for chars in a string in swift

//iterator for chars in a string
struct StrIter {
    
    let string:String
    var current:String.Index
    
    init(string:String) {
        self.string = string
        self.current = self.string.startIndex
    }
    
    mutating func next() -> Character? {
        if self.current != self.string.endIndex {
            let returned = self.string[self.current]
            self.current = self.string.index(after: self.current)
            return returned
        }
        else {
            return nil
        }
    }
}