18. November 2016

Do

A short snippet from the very useful Then library. Do something with an object in a block.

protocol Do {}
extension Do {
	public func `do`(_ block: (Self) -> Void) {
		block(self)
	}
}

extension NSObject: Do {}

This is useful for example with appearance proxies.

UITabBar.appearance().do {
	$0.barStyle = .black
	$0.isTranslucent = true
	$0.tintColor = UIColor.blue
}

Or the slightly more readable version.

UITabBar.appearance().do { tabBar in
	tabBar.barStyle = .black
	tabBar.isTranslucent = true
	tabBar.tintColor = UIColor.blue
}

Via Then.