diff --git a/codes/ExtensionStoredProperty.swift b/codes/ExtensionStoredProperty.swift new file mode 100644 index 0000000..500bc5a --- /dev/null +++ b/codes/ExtensionStoredProperty.swift @@ -0,0 +1,45 @@ +// Did you never asked how to add stored properties to extensions in Swift? ;) +// One of the benfits of Objective C and key-value coding feature code +// is still valid in Swift is you can we could add stored values to an object at runtime. +// Say welcome to associated object in Swift. + +import Foundation + +class MyClass { +} + +// We declared the type of key as Void?, which means a pointer to anything +private var key: Void? + +extension MyClass { + var title: String? { + get { + // Then & operator is used to get key address and send + // into the method as UnsafePointer parameter. + // associated object API get method. + return objc_getAssociatedObject(self, &key) as? String + } + set { + // associated object API set method. + objc_setAssociatedObject(self, &key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) + } + } +} + +// Test function +func printTitle(input: MyClass) { + if let title = input.title { + print("Title: \(title)") + } else { + print("not set") + } +} + +let myClassObject = MyClass() +printTitle(input: myClassObject) + +myClassObject.title = "Swift Daily Tips 1" +printTitle(input: myClassObject) + +myClassObject.title = "Swift Daily Tips 2" +printTitle(input: myClassObject) \ No newline at end of file diff --git a/screenshots/ExtensionStoredProperty.png b/screenshots/ExtensionStoredProperty.png new file mode 100644 index 0000000..3a03e6a Binary files /dev/null and b/screenshots/ExtensionStoredProperty.png differ