1

Class B is subclass of class A (B : A)

I initialise the class as B.init()

When invoked in A, the overriden method() is never called unless I use the cast:

class A {
    ...
    self.method() //A method called
    (self as! B).method() //B method called
}

Why?

Since I initialise the instance as B, I expect it's calling the overriden method, but this is not true, unless I use the cast.

aneuryzm
  • 63,052
  • 100
  • 273
  • 488
  • I think there's something going on that's not obvious from what you've posted. In the simplest case, the B method will be called. – Phillip Mills Aug 17 '16 at 12:44
  • Duplicate of [Overriding methods in Swift extensions](http://stackoverflow.com/questions/38213286/overriding-methods-in-swift-extensions) ([see below comment thread](http://stackoverflow.com/questions/38996783/overriden-method-not-called#comment65346443_38997022)) – Hamish Aug 17 '16 at 13:16
  • Who mentioned anything about extensions ? – Duncan Groenewald Aug 20 '18 at 12:38

1 Answers1

0

I think you have some mistake behind scenes. I constructed your example and it works as expected:

class A {
    func method() {
        print("A")
    }
    func check() {
        method()
        (self as? B)?.method()
    }
}
class B: A {
    override func method() {
        print("B")
    }
}

Somewhere else:

let b = B()
b.check()
//B
//B
Yury
  • 6,044
  • 3
  • 19
  • 41
  • It should also work if `check` simply calls `self.method()`. – Phillip Mills Aug 17 '16 at 12:45
  • @PhillipMills it work with `self` also, since `self` added implicitly – Yury Aug 17 '16 at 12:49
  • I've figured it out. It seems that that the method is not called if it's in an extension of B (it was my case). So, if I add the method in the extension B, instead of in class B, it's not called. Why? It should still override class A method, or not? – aneuryzm Aug 17 '16 at 12:51
  • 1
    @Patrick declarations in extensions can't override, so that sounds weird. – dfrib Aug 17 '16 at 12:52
  • 1
    @Patrick If your class inherits from `NSObject`, then see http://stackoverflow.com/questions/38213286/overriding-methods-in-swift-extensions – otherwise it shouldn't compile as you cannot override methods in extensions. – Hamish Aug 17 '16 at 13:03
  • @dfri My class inherits from UIViewController, so NSObject. – aneuryzm Aug 17 '16 at 13:14
  • @Harmish Thanks for the link. That's indeed my issue. – aneuryzm Aug 17 '16 at 13:14
  • 2
    @Patrick that is a non-general Swift case; for future reference, please mention such (relevant) details in your questions. Anyway, with that additional detail, the link provided by Hamish should be a target for dupe marking this thread. – dfrib Aug 17 '16 at 13:15
  • @dfri I agree, this is a duplicate. Unfortunately it's hard to think about all details: if I had realised those details by myself without help, I would probably not posted the question. – aneuryzm Aug 17 '16 at 13:26
  • @Patrick finding a dupe is not the easiest of tasks when pondering over something, but usually someone else can help out (like Hamish, in this case :). Also, sometimes when narrowing down a questoin to a minimal example, one runs the risk of removing a little to many details. Just take with you that many things that seems as peculiar in Swift (w.r.t. language guide/official sources) occur in the interjoining of Swift and Obj-C, e.g. for classes subclassing `NSObject`. – dfrib Aug 17 '16 at 13:33
  • 1
    @dfri I'm not sure I've understood your point here ;) I've read all documentation you mentioned already. I've asked a question in the community, since I could not figure out a solution to my problem. Hamish passed me a link (that I could not find, without a further investigation in my problem thanks to the help from others). Of course, if I had thought all details in advance I would have written them, but I didn't think about them. So that's what a community like Stack Overflow is useful for. Isn't? :) – aneuryzm Aug 17 '16 at 13:49
  • 1
    @Patrick My previous comment was meant as _"Don't worry, finding existing SO threads that might answer one's question can be hard, as well also asking a perfectly spot-on question with all necessary (but not redundant) details! Just take with you the insight of Obj-C/Swift peculiarities as they pop up a little here and there!"_, and was in no way criticising this question. But maybe the comment was a bit verbose and the _"Don't worry"_ intent was lost! :) – dfrib Aug 17 '16 at 13:53