【发布时间】:2015-02-22 11:11:45
【问题描述】:
嗯,这可能是一个重复的问题。
我发现了一些类似这样的问题:
Is there a way to set associated objects in Swift?
但是,我想在 swift 的 extension 中添加一个 Int 属性,而上面链接中的这些答案不起作用。
这是我的代码:
import ObjectiveC
var xoAssociationKey: UInt8 = 0
extension NSData {
var position: Int {
get {
return objc_getAssociatedObject(self, &xoAssociationKey) as Int
}
set {
objc_setAssociatedObject(self, &xoAssociationKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
}
}
override convenience init() {
self.init()
position = 0
}
}
我每次访问position时都会得到fatal error: unexpectedly found nil while unwrapping an Optional value
仅供参考,我确实在 Objective C 中找到了解决此错误的方法,我正在寻找一个快速的解决方案。如果您有兴趣,这是我在目标 C 中的代码:
static char PROPERTY_KEY;
@implementation NSData (Extension)
@dynamic position;
- (NSInteger)position {
return [objc_getAssociatedObject(self, &PROPERTY_KEY) integerValue];
}
- (void)setPosition:(NSInteger)position {
// Must convert to an object for this trick to work
objc_setAssociatedObject(self, &PROPERTY_KEY, @(position), OBJC_ASSOCIATION_COPY);
}
- (instancetype)init {
self = [super init];
if (self) {
self.position = 0;
}
return self;
}
【问题讨论】: