If you take a class that cannot be visible to Obj-c:
public final class SwiftOnlyClass <T> {
// ...
}
and add an extension marked @objc, its static extension methods are still not visible to Obj-c:
@objc
public extension SwiftOnlyClass {
@objc
static var helloWorld: String { // no way to call this from Obj-c code, e.g. [SwiftOnlyClass helloWorld];
return "Hello world"
}
}
I guess this makes since, since SwiftOnlyClass isn't visible for Obj-c to actually call any static methods on it. Should the compiler disallow this to avoid confusion?
In Xcode Version 10.2 (10E125) it produces an error already Members of extensions of generic classes cannot be declared @objc
If you remove <T> you can call that static method, for example:
swift file:
import Foundation
public class Foo: NSObject {
@objc class func otherClass() -> Any {
return SwiftOnlyClass.self
}
}
public final class SwiftOnlyClass {
}
@objc
public extension SwiftOnlyClass {
@objc
static var helloWorld: String {
return "Hello world"
}
}
Ah you're right, I was actually working with a non-generic Swift class, but added the <T> to be clear it was Swift-only. I'd think either Xcode should allow me to call [SwiftOnlyClass helloWorld]; without performSelector, or have the compiler throw an error. It's strange that neither happen.