Skip to content

Instantly share code, notes, and snippets.

@hisoka0917
Last active January 26, 2018 03:29
Show Gist options
  • Save hisoka0917/d9c507c0dc94b7cc01ef62431bf57a81 to your computer and use it in GitHub Desktop.
Save hisoka0917/d9c507c0dc94b7cc01ef62431bf57a81 to your computer and use it in GitHub Desktop.
Extension property in Swift
extension NSObject {
private struct AssociatedKeys {
static var extensionProps: UInt8 = 0
}
private var extensionProps: NSObject? {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.extensionProps) as? NSObject
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.extensionProps, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment