Created
November 29, 2014 17:09
-
-
Save nafu/8ded244dd4f670563d1c to your computer and use it in GitHub Desktop.
Swift Sequence
This file contains 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
// Swift Sequence | |
import UIKit | |
var str = "Hello, playground" | |
class Cart<T> { | |
var items: [T] | |
init(items: [T]) { | |
self.items = items | |
} | |
} | |
extension Cart: SequenceType { | |
typealias Generator = GeneratorOf<T> | |
func generate() -> GeneratorOf<T> { | |
var i = 0 | |
return GeneratorOf { return i >= self.items.count ? nil : self.items[i++] } | |
} | |
} | |
var cart = Cart(items: ["foo", "bar", "baz"]) | |
for item in cart { | |
println(item) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment