I have written a class in Swift in my existing Objective-C project. So far, the bridging has worked very well. I do have a method however, where I generate a class at runtime using NSClassFromString(). When the string is my Swift class name, it returns nil.
class MySwiftClass : NSObject {}
and in Objective-C:
Class myClass = NSClassFromString(@"MySwiftClass");
and myClass would be nil every time. I've also tried:
Class myClass = NSClassFromString(@"MyAppName.MySwiftClass");
and still nil.
All swift classes use the Product Module Name a dot and the classname for their namespace (Module.Class). If you wanted to use "MySwiftClass" name for the class within your Objective-C code; you can add @objc(MySwiftClass)
annotation to expose the same swift class name (without the module):
@objc(MySwiftClass)
class MySwiftClass{
...
}
Then
Class myClass = NSClassFromString(@"MySwiftClass");
Will contain the class instead of being nil.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With