从Swift类访问Objective-c基类的实例变量
问题描述:
拥有Objective c基类:
Having an Objective c base class:
@interface ObjcClass : NSObject {
NSString *aVariable_;
}
快速的子类:
class SwiftClass : ObjcClass {
func init() {
// aVariable_ can't be accessed here. An Objective-c derived
// class has direct access to it's super's instance variables!
}
}
如何访问 ObjcClass
aVariable _
来自 SwiftClass
?
答
很棒的查询。我们努力完成这项工作。我找到的唯一可行解决方案
Great query. We have tried to hard to get this done. The only working solution I found
使用 self.valueForKey(aVariable _)获取价值
使用设置值self.setValue(新值,forKey:aVariable _)
希望有所帮助。在不改变超类的情况下可能的解决方案。
Hope that helps. Possible solution without altering super class.