Created
July 3, 2018 22:31
-
-
Save mike3k/672d437a5eec448e2f06d723ade83e14 to your computer and use it in GitHub Desktop.
least ugly way to add a property to a class extension
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
private var fooKey = 0 | |
extension UIViewController { | |
var foo: Bool { | |
set { | |
objc_setAssociatedObject(self, &fooKey, NSNumber(value: newValue), .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | |
} | |
get { | |
return objc_getAssociatedObject(self, &fooKey) as? Bool ?? false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment