Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
366 views
in Technique[技术] by (71.8m points)

ios - Swift call class function from corresponding subclass in superclass function

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 the superclass at runtime.

MySuperClass

class func dummyDict() -> NSDictionary

init(coder aDecoder: NSCoder!) {

    for(key,value) in self.class.dummyDict(){
                      --------------------
                               ^
                               |
                               |
                 Get this from the corresponding subclass at runtime!

        NSLog("encoding (value) for key (key)")
    }

}

Is it possible that subclasses from MySuperClass access the class function dummyDict() at runtime ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think I caught what you mean. You create a Base class, implementing an initializer and a class (static) function:

class Base {
    class func dummyDict() -> Dictionary<String, String> {
        return ["base1": "val1"]
    }

    init() {
        for (key, value) in self.dynamicType.dummyDict() {
            println("encoding (value) for key (key)")
        }
    }
}

Next you want to create subclasses, and have the initializer to call an overridden version of the dummyDict method. You simply have to override that method:

class Subclass1 : Base {
    override class func dummyDict() -> Dictionary<String, String> {
        return ["subclass1": "sub1"]
    }
}

Now, when you create an instance of Subclass1, what's printed is:

encoding sub1 for key subclass1

which is the expected output.

Note the for loop in the initializer is using self.dynamicType.dummyDict() rather than Base.dummyDict(). The latter always calls the class method defined in the Base class, whereas the former calls it in the scope of the actual class inherited from Base


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...