Objc protocol optional method names when inheriting a generic class

I stumbled across this behaviour a few days ago, took me quite a while to figure out what was happening :sweat_smile::

import Foundation

@objc protocol P { @objc(doF) optional func f() -> Int }
class Gen<T>: NSObject, P {}
class NoGen:  NSObject, P {}

class NoGen1: NoGen {
    func f() -> Int {
        return 42
    }
}
(NoGen1() as P).f?() // 42, works

class Gen1: Gen<Any> {
    func f() -> Int {
        return 42
    }
}
(Gen1() as P).f?() // nil <----------

class Gen2: Gen<Any> {
    @objc(doF) func f() -> Int { // explicitly set objc name
        return 42
    }
}
(Gen2() as P).f?() // 42, works again

Seems like the information about the proper @objc names gets lost for subclasses of generic classes.

Is this a known bug or limitation?

Bug, I think! And I didn't see one in a quick JIRA search that covered it, so please file a new one!

Done :+1:

1 Like
Terms of Service

Privacy Policy

Cookie Policy