Skip to content

Instantly share code, notes, and snippets.

@seanlilmateus
Last active August 29, 2015 14:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanlilmateus/9bd676e0421cd5ea1135 to your computer and use it in GitHub Desktop.
Save seanlilmateus/9bd676e0421cd5ea1135 to your computer and use it in GitHub Desktop.
`tap:` method for Swift borrowed from Ruby. ( a little broken, Cast needed)
import Cocoa
extension NSObject {
func tap(blk:(AnyObject) -> Void) -> Self {
blk(self as NSObject)
return self
}
}
// Alternative, you will need to specify the type :: view:NSView = ...
extension NSObject {
func tap<T>(blk:(T) -> Void) -> T {
blk(self as T)
return self as T
}
}
let nsarray:NSMutableArray = NSMutableArray(array: [1,2,3,4,5]).tap { x in
x.replaceObjectAtIndex(2, withObject:"Record")
}
println("NSArray \(nsarray)")
let view:NSView = NSView(frame: NSRect(x:0, y:0, width:10, height:10)).tap { v in
var mv = v as NSView
mv.hidden = false // to set hidden, we need to cast as NSView :(
println("VIEW \(v.frame)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment