Skip to content

Instantly share code, notes, and snippets.

@hoorace
Created August 24, 2015 02:23
Show Gist options
  • Select an option

  • Save hoorace/01c35710abaeb8f29c47 to your computer and use it in GitHub Desktop.

Select an option

Save hoorace/01c35710abaeb8f29c47 to your computer and use it in GitHub Desktop.
import UIKit
@objc class ClosureDispatch {
init(f:()->()) { self.action = f }
func execute() -> () { action() }
let action: () -> ()
}
var redBlueGreen:[String] = ["Red", "Blue", "Green"]
let buttons:[UIButton] = map(0..<redBlueGreen.count) { i in
let text = redBlueGreen[i]
var btn = UIButton(frame: CGRect(x: i * 50, y: 0, width: 100, height: 44))
btn.setTitle(text, forState: .Normal)
btn.setTitleColor(UIColor.redColor(), forState: .Normal)
btn.backgroundColor = UIColor.lightGrayColor()
return btn
}
let functors:[ClosureDispatch] = map(buttons) { btn in
let functor = ClosureDispatch(f:{ [unowned btn] in
println("Hello from \(btn.titleLabel!.text!)") })
btn.addTarget(functor, action: "execute", forControlEvents: .TouchUpInside)
return functor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment