通过子类化将其添加到类中-Swift

问题描述 投票:0回答:1

我正在尝试通过将其子类化为类,向类中添加其他变量:

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的操作。每个实例的此变量也可以不同(它不是常数)。

ios swift inheritance instance subclass
1个回答
0
投票

删除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)
}
© www.soinside.com 2019 - 2024. All rights reserved.