Last active
July 4, 2023 07:12
-
-
Save maxcampolo/8493e7a1bc9854b67d5f to your computer and use it in GitHub Desktop.
Extension that adds a tag property to UIViewController
This file contains 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
/* | |
* | |
* This class provides a UIViewController extension which adds a tag property to all UIViewController subclasses. | |
* | |
*/ | |
import Foundation | |
private var tagAssociationKey: UInt8 = 0 | |
extension UIViewController { | |
public var tag: String! { | |
get { | |
return objc_getAssociatedObject(self, &tagAssociationKey) as? String | |
} | |
set(newValue) { | |
objc_setAssociatedObject(self, &tagAssociationKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN)) | |
} | |
} | |
} |
update for Swift 5
public var tag: String {
get {
return objc_getAssociatedObject(self, &tagAssociationKey) as? String ?? "❤️"
}
set(newValue) {
objc_setAssociatedObject(self, &tagAssociationKey, newValue, .OBJC_ASSOCIATION_RETAIN)
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Objective-C?