Swift call class function from corresponding subclass in superclass function

前端 未结 3 2216
迷失自我
迷失自我 2020-12-15 11:58

I would like to implement init(coder aDecoder: NSCoder!) in a superclass, and use it in all subclasses by calling a class method on the particular subclass in t

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 12:30

    dynamicType is deprecated in Swift 3. We must use type(of:).

    So Antonio's example is now:

    class Base {
        class func dummyDict() -> [String: String] {
            return ["base1": "val1"]
        }
    
        init() {
            for (key, value) in type(of: self).dummyDict() {
                print("encoding \(value) for key \(key)")
            }
        }
    }
    
    class Subclass1 : Base {
        override class func dummyDict() -> [String: String] {
            return ["subclass1": "sub1"]
        }
    }
    

提交回复
热议问题