Last active
February 18, 2019 14:45
-
-
Save pedrommcarrasco/34c6f225f9c0285351fdd41cd7a20dc1 to your computer and use it in GitHub Desktop.
How to be Lazy in Extensions - Solution
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
public extension UIView { | |
private struct AssociatedKey { | |
static var constrictor: UInt8 = 0 | |
} | |
var constrictor: Constrictor { | |
if let constrictor = objc_getAssociatedObject(self, &AssociatedKey.constrictor) as? Constrictor { | |
return constrictor | |
} else { | |
let constrictor = Constrictor(object: self) | |
objc_setAssociatedObject(self, &AssociatedKey.constrictor, constrictor, .OBJC_ASSOCIATION_RETAIN) | |
return constrictor | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment