我正在尝试通过将其子类化为类,向类中添加其他变量:
class MySubclass : MySuperClass {
var newVar: SomeType = .unknown
override class func someFunc(for someVar: SomeType) -> UIImage? {
self.newVar = someVar
return super.someFunc(for: someVar)
}
}
但是,出现以下错误:Instance member newVar cannot be used on Type MySubclass
我的目标是使newVar
可用于该类的所有实例,以便我可以执行类似myInstance.newVar
的操作。每个实例的此变量也可以不同(它不是常数)。
删除class
override func someFunc(for someVar: SomeType) -> UIImage? {
或添加罪状
class MySubclass : MySuperClass {
static let shared = MySubclass()
var newVar: SomeType = .unknown
override class func someFunc(for someVar: SomeType) -> UIImage? {
MySubclass.shared.newVar = someVar
return super.someFunc(for: someVar)
}