Is there a way to annotate a fast method requiring calling its superclass

I have a superclass, which has a basic implementation method ( prepareForSegue:sender: in my case), and I want every subclass that overrides this method is a super-realization.

 class A { func foo() { // Some basic shared behavior } } class B : A { override func foo() { // I want the compiler to yell at me if I forget super.foo() here } } 

Is there a way to use NS_REQUIRES_SUPER in a fast or perhaps way to simulate this behavior using protocols?

+5
source share
1 answer

There may be a way this may work:

 class A { private func foo() { println("A: Foo") } final func bar() { println("A: Bar") foo() } } class B : A { override private func foo() { println("B: foo") } } var a = A() a.bar() // "A: Bar\nA: Foo" println() var b = B() b.bar() // "A: Bar\nB: Foo" 

This code forbids overriding the bar() method, but allows you to override the foo() method. Then the trick is that the user needs to call the bar() method instead of the foo() method, but if you do, then you will make sure that the superclass method is always used, i.e. println("A: Bar") !: -)

0
source

Source: https://habr.com/ru/post/1208082/


All Articles