Last active
February 25, 2019 07:18
-
-
Save mendesbarreto/91ae2cde67aad91ed9f9773062f9a575 to your computer and use it in GitHub Desktop.
Exemplo how to use GeneratorType
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Prefix Class Generator | |
class StringPrefixGenerator: GeneratorType { | |
typealias Element = String | |
let string: String | |
var offset: String.Index | |
init(string:String) { | |
self.string = string | |
self.offset = string.startIndex | |
} | |
/** | |
Get always the next letter at the sentence and return new prefix based on previous one | |
- returns: returns new prefix | |
*/ | |
func next() -> Element? { | |
guard offset < string.endIndex else { | |
return nil | |
} | |
offset = offset.successor() | |
return string[ string.startIndex..<offset ] | |
} | |
} | |
let prefixGenerator = StringPrefixGenerator(string: "Douglas Mendes Barreto") | |
prefixGenerator.next() //print D | |
prefixGenerator.next() //print Do | |
prefixGenerator.next() //print Dou |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment