Created
April 17, 2016 19:20
-
-
Save wh1pch81n/4c8c1578241274fca13e8c1b9871e217 to your computer and use it in GitHub Desktop.
Sometimes it is easier to read code where the set up is in a block.
This file contains 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 UIKit | |
/** | |
You can use this to initalize an object (class or struct) while containing the set up code for it in a block | |
*/ | |
@warn_unused_result | |
func setUpObject<T>(obj: T, withBlock setUp: (inout T) -> ()) -> T { | |
var _obj = obj | |
setUp(&_obj) | |
return _obj | |
} | |
// example with class | |
var red = setUpObject(UIView()) { | |
$0.frame.size = CGSize(width: 100, height: 100) | |
$0.backgroundColor = .purpleColor() | |
} | |
// example with struct | |
let hi = setUpObject(Array(arrayLiteral: "hello")) { | |
$0.append("world") | |
} | |
// Class example | |
class Foo { | |
let it = setUpObject(UIView()) { | |
$0.frame.size = CGSize(width: 100, height: 100) | |
$0.backgroundColor = UIColor.blackColor() | |
} | |
let hi = setUpObject(["hello"] as [String]) { | |
$0.append("world") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment