Skip to content

Instantly share code, notes, and snippets.

@MMnasrabadi
Forked from AliSoftware/using.swift
Created June 20, 2020 12:57
Show Gist options
  • Save MMnasrabadi/161b29b60005d9fbabb1a7c5f4d1415c to your computer and use it in GitHub Desktop.
Save MMnasrabadi/161b29b60005d9fbabb1a7c5f4d1415c 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