See below code.

protocol Foo {

    func bar()

}


extension Foo {

    func bar() {

        print("I am bar.")

    }

}


class A:Foo {

    func output() {

        print(type(of:self)) // prints "B".

        self.bar() // prints "I am bar."

        (self as! B).bar() // prints "I am B."

    }

}


class B:A {

    func bar() {

        print("I am B.")

    }

}


let b = B()

b.output()


I thought `self.bar()` would do the same as `(self as! B).bar()`. It
didn't. In my opinion,  `type(of:self) is B.type`, so they should be the
same, shouldn't they?


Zhaoxin
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

Reply via email to