Created
January 28, 2019 07:35
-
-
Save joshuajhomann/df65ddaf2765392aa2d671cba5dd0dda to your computer and use it in GitHub Desktop.
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 ObjectiveC | |
public protocol DynamicProperties: class { | |
subscript<T>(dynamic key: String) -> T? { get set } | |
} | |
private extension String { | |
var unsafePointer: UnsafeRawPointer { | |
return UnsafeRawPointer(bitPattern: hashValue)! | |
} | |
} | |
public extension DynamicProperties { | |
public subscript<T>(dynamic key: String) -> T? { | |
get { return objc_getAssociatedObject(self, key.unsafePointer) as? T } | |
set { objc_setAssociatedObject(self, key.unsafePointer, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } | |
} | |
} | |
extension NSObject: DynamicProperties { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment