Forked from amelnikov78/gist:9f74971b591cd837e0690208ef86849f
Created
May 17, 2022 07:15
-
-
Save EkkoG/7da9cfe60d3bee32f40248da5066f964 to your computer and use it in GitHub Desktop.
Swift extensions *can* add stored properties
This file contains hidden or 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 Foundation | |
func associatedObject<ValueType: AnyObject>( | |
base: AnyObject, | |
key: UnsafePointer<UInt8>, | |
initialiser: () -> ValueType) | |
-> ValueType { | |
if let associated = objc_getAssociatedObject(base, key) | |
as? ValueType { return associated } | |
let associated = initialiser() | |
objc_setAssociatedObject(base, key, associated, | |
.OBJC_ASSOCIATION_RETAIN) | |
return associated | |
} | |
func associateObject<ValueType: AnyObject>( | |
base: AnyObject, | |
key: UnsafePointer<UInt8>, | |
value: ValueType) { | |
objc_setAssociatedObject(base, key, value, | |
.OBJC_ASSOCIATION_RETAIN) | |
} | |
///////////////////////////////////////////////////// | |
class Miller {} // Here's the class we will extend | |
class Cat { // Every Miller should have a Cat | |
var name = “Puss” | |
} | |
private var catKey: UInt8 = 0 // We still need this boilerplate | |
extension Miller { | |
var cat: Cat { // cat is *effectively* a stored property | |
get { | |
return associatedObject(self, key: &catKey) | |
{ return Cat() } // Set the initial value of the var | |
} | |
set { associateObject(self, key: &catKey, value: newValue) } | |
} | |
} | |
let grumpy = Miller() | |
grumpy.cat.name // shows "Puss" | |
grumpy.cat.name = “Hephaestos” | |
grumpy.cat.name // shows "Hephaestos" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment