Created
January 9, 2018 08:39
-
-
Save lamlv2305/b0e976372f378061d83d8dc8b4a8bf3f to your computer and use it in GitHub Desktop.
[SWIFT 4.0] New variable in extension for existed class
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
class Testing { | |
} | |
private var key: Void? | |
extension Testing { | |
var title: String { | |
get { | |
guard let value = objc_getAssociatedObject(self, &key) as? String else { | |
return "" | |
} | |
return value | |
} | |
set { | |
objc_setAssociatedObject(self, &key, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment