Property in a protocol extension achieved using associated object
suggest changeIn Swift, protocol extensions cannot have true properties.
However, in practice you can use the “associated object” technique. The result is almost exactly like a “real” property.
Here is the exact technique for adding an “associated object” to a protocol extension:
The implementation calls the Objective-C runtime functions objc_getAssociatedObject and objc_setAssociatedObject.
The basic calls are:
get {
return objc_getAssociatedObject(self, & _Handle) as! YourType
}
set {
objc_setAssociatedObject(self, & _Handle, newValue, .OBJC_ASSOCIATION_RETAIN)
}
Here’s a full example. The two critical points are:
- In the protocol, constrain to a class (
: class, orAnyObjectin Swift 4 and later). A value type cannot use associated objects. - In the extension, use
where Self: UIViewController(or another class) so the conforming type is a class instance.
So, for an example property “p”:
import Foundation
import UIKit
import ObjectiveC // don't forget this
var _Handle: UInt8 = 42 // it can be any value
protocol Able: class {
var click:UIView? { get set }
var x:CGFloat? { get set }
// note that you >> do not << declare p here
}
extension Able where Self:UIViewController {
var p:YourType { // YourType might be, say, an Enum
get {
return objc_getAssociatedObject(self, & _Handle) as! YourType
// HOWEVER, SEE BELOW
}
set {
objc_setAssociatedObject(self, & _Handle, newValue, .OBJC_ASSOCIATION_RETAIN)
// often, you'll want to run some sort of "setter" here...
__setter()
}
}
func __setter() { something = p.blah() }
func someOtherExtensionFunction() { p.blah() }
// it's ok to use "p" inside other extension functions,
// and you can use p anywhere in the conforming class
}
In any conforming class, you have now “added” the property “p”:
You can use “p” just as you would use any ordinary property in the conforming class. Example:
class Clock:UIViewController, Able {
var u:Int = 0
func blah() {
u = ...
... = u
// use "p" as you would any normal property
p = ...
... = p
}
override func viewDidLoad() {
super.viewDidLoad()
// "p" must be given a value before it is read. `.none` is only valid if that is a real value of the property's type.
p = ...
}
}
Note. You MUST initialize the pseudo-property.
Xcode will not enforce you initializing “p” in the conforming class.
It is essential that you initialize p, perhaps in viewDidLoad of the conforming class.
It is worth remembering that p is actually just a computed property. p is actually just two functions, with syntactic sugar. There is no p “variable” anywhere: the compiler does not “assign some memory for p” in any sense. For this reason, it is meaningless to expect Xcode to enforce “initializing p”.
Indeed, to speak more accurately, you must remember to “use p for the first time, as if you were initializing it”. (Again, that would very likely be in your viewDidLoad code.)
Regarding the getter as such.
Note that it will crash if the getter is called before a value for “p” is set.
To avoid that, consider code such as:
The getter below is the safe pattern. YourType needs a value you can create, or some other default.
get {
if let existing = objc_getAssociatedObject(self, &_Handle) as? YourType {
return existing
}
let defaultValue = YourType()
objc_setAssociatedObject(self, &_Handle, defaultValue, .OBJC_ASSOCIATION_RETAIN)
return defaultValue
}
To repeat. Xcode will not enforce you initializing p in the conforming class. It is essential that you initialize p, say in viewDidLoad of the conforming class.
Making the code simpler…
You may wish to use these two global functions:
func _aoGet(_ ss: Any!, _ handlePointer: UnsafeRawPointer!, _ safeValue: Any!)->Any! {
let g = objc_getAssociatedObject(ss, handlePointer)
if (g == nil) {
objc_setAssociatedObject(ss, handlePointer, safeValue, .OBJC_ASSOCIATION_RETAIN)
return safeValue
}
return objc_getAssociatedObject(ss, handlePointer)
}
func _aoSet(_ ss: Any!, _ handlePointer: UnsafeRawPointer!, _ val: Any!) {
objc_setAssociatedObject(ss, handlePointer, val, .OBJC_ASSOCIATION_RETAIN)
}
Note that they do nothing, whatsoever, other than save typing and make the code more readable. (They are essentially macros or inline functions.)
Your code then becomes:
protocol PMable: class {
var click:UILabel? { get set } // ordinary properties here
}
var _pHandle: UInt8 = 321
extension PMable where Self:UIViewController {
var p:P {
get {
return _aoGet(self, &_pHandle, P() ) as! P
}
set {
_aoSet(self, &_pHandle, newValue)
__pmSetter()
}
}
func __pmSetter() {
click!.text = String(p)
}
func someFunction() {
p.blah()
}
}
In the _aoGet example, P can be created with P(). A String, number, or any other default is fine too.