Skip to content

Instantly share code, notes, and snippets.

@pedrommcarrasco
Last active February 18, 2019 14:45
Show Gist options
  • Save pedrommcarrasco/34c6f225f9c0285351fdd41cd7a20dc1 to your computer and use it in GitHub Desktop.
Save pedrommcarrasco/34c6f225f9c0285351fdd41cd7a20dc1 to your computer and use it in GitHub Desktop.
How to be Lazy in Extensions - Solution
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