Skip to content

Instantly share code, notes, and snippets.

@gamako
Created May 28, 2016 14:24
Show Gist options
  • Save gamako/270c01681398dd4980e03f07bc67f149 to your computer and use it in GitHub Desktop.
Save gamako/270c01681398dd4980e03f07bc67f149 to your computer and use it in GitHub Desktop.
Ruby, Underscoreライクなtapメソッドのextension実装
import UIKit
protocol Tappable : class {
}
extension Tappable where Self : NSObject {
func tap(@noescape function: (Self) -> ()) -> Self {
function(self)
return self
}
}
extension NSObject : Tappable {}
extension UIView : Tappable {}
#if debug
func tap_example() {
UIView().tap { (a : UIView) in
print("\(a)")
}
UIStackView().tap { (a : UIStackView) in
print("\(a)")
}
let _ = UILabel().tap {
$0.text = "ほげほげ"
}
NSObject().tap { (a : NSObject) in
print("\(a)")
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment