Open onmyway133 opened 8 years ago
Need to make A subclass of NSObject
import Cocoa
@objc protocol P: class {
optional func f()
}
class A: NSObject, P {
}
extension A {
func f() {
print("A")
}
}
class B: A {
override func f() {
super.f()
print("B")
}
}
let a = A()
a.f()
let b = B()
b.f()
It can be in extension
extension B {
override func f() {
super.f()
print("B in extension")
}
}
Everytime i google "declarations in extensions cannot override yet" 🤣 Thx!
@eonist thanks for reading my diary 😇
☝️Although it can be achieved with out extending NSObject. By using a protocol and shadowing the method call in an extension. For when I google this again 😛
@eonist can you elaborate a bit ? I'm having this issue and don't understand your solution
@rafaelnobrepd Here is a clue: https://developer.apple.com/videos/play/wwdc2015-408/?time=1767
If you cannot change classes A
and B
directly (e.g. because they belong to some framework like UIKit
):
protocol P {
func f()
}
class A {}
class B: A {}
extension P {
func f() {
print("a")
}
}
extension A: P {}
extension B {
func f() {
super.f()
print("b")
}
}
let a = A()
let b = B()
a.f()
b.f()