Created
June 5, 2015 20:39
-
-
Save endash/ea75a61c90de30a4bb73 to your computer and use it in GitHub Desktop.
Instantiate a view from a nib/xib in Swift, with the current API
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
extension NSView { | |
class func instantiateViewFromNib<T>(named nibName: String, inBundle bundle: NSBundle = NSBundle.mainBundle()) -> T? { | |
var objects : NSArray? | |
if bundle.loadNibNamed(nibName, owner: nil, topLevelObjects: &objects) { | |
if let objects = objects { | |
for object in objects { | |
if let object = object as? T { | |
return object | |
} | |
} | |
} | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment