Disclaimer: I know that this is a pretty old version of Typhoon, but I'm trying to add a Swift class to an existing Objective-C codebase, and I'm curious if this could work (in theory, I think it should).
I have the following Swift Cocoa class:
import Cocoa
public class MyClass: NSObject {
}
I would like to use it as a dependency in an Objective-C class:
@interface DependentClass()
@property (nonatomic, strong, readonly) MyClass *myClass;
@end
- (instancetype)initWithMyClass:(MyClass *)aClass {
self = [super init];
if (self) {
_myClass = aClass;
}
return self;
}
In my XML configuration, I register the component, and the dependent class:
<component class='MyClass' key='myClass' scope='singleton'>
</component>
<component class='DependentClass' key='dependentClass' scope='singleton'>
<initializer selector='initWithMyClass:'>
<argument parameterName='myClass' ref='myClass'/>
</initializer>
</component>
When I launch the application, it crashes while resolving myClass:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Class 'MyClass' can't be resolved.'
Is there a way to make it work? As far as I know the only thing I have to do is make MyClass
an NSObject
subclass (either my sublassing, or using @objc).
This will work, but the name of the Swift class will be be auto-namespaced to app/frameworkname.ClassName.
You could test that at runtime using NSStringFromClass(MyClass.self)