onmyway133 / notes

:notebook_with_decorative_cover: Issues and solutions I found during development, mostly iOS
https://onmyway133.com/
MIT License
63 stars 4 forks source link

Declarations from extensions cannot be overridden yet #86

Open onmyway133 opened 8 years ago

onmyway133 commented 8 years ago
@objc protocol P: class {
  optional func f()
}

class A: P {

}

extension A {
  func f() {
    print("A")
  }
}

class B: A {
  override func f() { // Declarations from extensions cannot be overridden yet 
    super.f() 
    print("B")
  }
}

let a = A()
a.f()
onmyway133 commented 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()
onmyway133 commented 8 years ago

It can be in extension

extension B {
  override func f() {
    super.f()
    print("B in extension")
  }
}
eonist commented 7 years ago

Everytime i google "declarations in extensions cannot override yet" 🤣 Thx!

onmyway133 commented 7 years ago

@eonist thanks for reading my diary 😇

eonist commented 7 years ago

☝️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 😛

rafaelnobrepd commented 7 years ago

@eonist can you elaborate a bit ? I'm having this issue and don't understand your solution

eonist commented 7 years ago

@rafaelnobrepd Here is a clue: https://developer.apple.com/videos/play/wwdc2015-408/?time=1767

fabb commented 6 years ago

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()