A followup to my previous question. You can paste this into a macOS playground. As soon as I make the protocol @objc the check exporter.type is User.Type fails. Is there a way to retrieve the type information and get the desired result? (I guess @jrose would know?)
import Foundation
//@objc(Exporter)
protocol Exporter: NSObjectProtocol {
var type: Any { get }
func export(item: Any)
}
struct User { var name: String }
final class UserExporter: NSObject, Exporter {
var type: Any { return User.self }
func export(item: Any) {
guard let u = item as? User else {
return print("That's not a user")
}
print("Got user: \(u.name)")
}
}
let user = User(name: "Kermit")
let exporter: Exporter = UserExporter()
if exporter.type is User.Type {
print("Correct exporter")
}
exporter.export(item: user)
On a sidenote: Is loading a type at runtime only being available to obj-c a shortcoming of the Swift runtime or only because of how Bundle is implemented (talking about casting a runtime loaded Bundle’s principalClass for example)