Skip to content

Instantly share code, notes, and snippets.

@nafu
Created November 29, 2014 17:09
Show Gist options
  • Save nafu/8ded244dd4f670563d1c to your computer and use it in GitHub Desktop.
Save nafu/8ded244dd4f670563d1c to your computer and use it in GitHub Desktop.
Swift Sequence
// 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