There is a such method in Bundle:
func classNamed(_ className: String) -> AnyClass?
The description says it loads the Class
object for className
. It's, obviously, an Objective-C stuff. I started from Objective-C but didn't used it, preferring NSClassFromString.
Now I suddenly tested it in various applications. I was surprised that it doesn't work in iOS apps neither in Playground:
import Foundation
class TalkingFruit {
func greet() {
print("Hello, playground")
}
}
@objc class LagacyFruit: NSObject {
}
print(Bundle.main.classNamed("TalkingFruit") ?? "no class")
// no class
print(Bundle.main.classNamed("LegacyFruit") ?? "no class")
// no class
print(Bundle.main.classNamed("NSObject") ?? "no class either")
// no class either
And now I have a question: Does it even work? And how it's supposed to be used? Working use case example would be great.