Skip to content

Instantly share code, notes, and snippets.

@AliSoftware
Last active June 20, 2020 12:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save AliSoftware/917e04c09ae15a5caedb to your computer and use it in GitHub Desktop.
Save AliSoftware/917e04c09ae15a5caedb to your computer and use it in GitHub Desktop.
func using<T: AnyObject>(object: T, execute: (T) throws -> Void) rethrows -> T {
try execute(object)
return object
}
import UIKit
// Then in some configureView() function of an UIViewController or whatnot…
let label1 = using(UILabel()) {
$0.backgroundColor = .blackColor()
$0.textColor = .whiteColor()
$0.textAlignment = .Center
$0.text = "Hello"
}
let label2 = using(UILabel()) {
$0.backgroundColor = .whiteColor()
$0.textColor = .blueColor()
$0.textAlignment = .Natural
$0.text = "World"
}
let view = using(UIView()) {
$0.backgroundColor = .yellowColor()
$0.addSubview(label1)
$0.addSubview(label2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment