Helper functions for associated objects! ๐
Add the following to Package.swift
:
.package(url: "https://github.com/stateman92/AssociatedObjects", exact: .init(0, 0, 2))
private struct AssociatedObjectKeys {
static var testValue = "testValue"
}
// Declaration somewhere, e.g. in another package ...
// protocol TestProtocol { }
extension TestProtocol {
var testValue: Int {
get {
getAssociatedObject(self, AssociatedObjectKeys.testValue, defaultValue: .zero)
}
nonmutating set {
setAssociatedObject(self, AssociatedObjectKeys.testValue, newValue)
}
}
}
final class Class: TestProtocol {
init() {
assert(testValue == .zero)
print(testValue)
let newValue = 10
testValue = newValue
assert(testValue == newValue)
print(testValue)
}
}
For details see the Example app.