我有一个ViewController,我使用关联对象添加了两个新属性:一个枚举和一个字符串(字符串版本取自here)
这是我的示例代码:
extension UIViewController {
private struct AssociatedKeys {
static var handle = "handle"
}
enum CustomStringEnum: String {
case One = "One"
case Two = "Two"
case Three = "Three"
}
var customEnum: CustomStringEnum {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.handle) as? CustomStringEnum ?? .One
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.handle, newValue.rawValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
var descriptiveName: String {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.handle) as! String
}
set {
objc_setAssociatedObject(
self,
&AssociatedKeys.handle,
newValue as NSString?,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let vc = UIViewController()
vc.customEnum = .Three
vc.descriptiveName = "Three"
print(vc.customEnum.rawValue) // -> This prints "One"
print(vc.descriptiveName) // -> This prints "Three"
}
}
字符串版本正常工作,但枚举版本没有。我不确定问题是什么。
objc_getAssociatedObject
或objc_setAssociatedObject
存在问题。获取版本似乎始终为零,因此返回默认值One。
答案 0 :(得分:3)
将您的代码更改为此
extension UIViewController {
private struct AssociatedKeys {
static var handle = "handle"
static var enumContext = "enumContext"
}
enum CustomStringEnum: String {
case One = "One"
case Two = "Two"
case Three = "Three"
}
var customEnum: CustomStringEnum {
get {
let rawvalue = objc_getAssociatedObject(self, &AssociatedKeys.enumContext)
if rawvalue == nil{
return .One
}else{
return CustomStringEnum(rawValue: rawvalue as! String)!;
}
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.enumContext, newValue.rawValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
var descriptiveName: String {
get {
return objc_getAssociatedObject(self, &AssociatedKeys.handle) as! String
}
set {
objc_setAssociatedObject(
self,
&AssociatedKeys.handle,
newValue as NSString?,
.OBJC_ASSOCIATION_RETAIN_NONATOMIC
)
}
}
}
然后它会起作用
答案 1 :(得分:-1)
您在.One
函数中对get
进行了硬编码。
根据您的评论,您对相关值的复杂性没有任何意义,您应该简化:
enum Numbers: String {
case One = "One"
case Two = "Two"
case Three = "Three"
// default
init() { self = .One }
static let germanNumbers = [One: "Eins", Two: "Zwei", Three: "Drei"]
var germanString: String { return Numbers.germanNumbers[self]! }
}
let num = Numbers.Three
print(num) // "Three"
print(num.rawValue) // "Three"
let defaultNum = Numbers()
print(defaultNum) // "One"
print(num.germanString) // "Drei"
print(defaultNum.germanString) // "Eins"