Last active
May 28, 2024 17:42
-
-
Save seanlilmateus/9bd676e0421cd5ea1135 to your computer and use it in GitHub Desktop.
`tap:` method for Swift borrowed from Ruby. ( a little broken, Cast needed)
This file contains hidden or 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
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