Skip to content

Instantly share code, notes, and snippets.

@lamlv2305
Created January 9, 2018 08:39
Show Gist options
  • Save lamlv2305/b0e976372f378061d83d8dc8b4a8bf3f to your computer and use it in GitHub Desktop.
Save lamlv2305/b0e976372f378061d83d8dc8b4a8bf3f to your computer and use it in GitHub Desktop.
[SWIFT 4.0] New variable in extension for existed class
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