I have a problem with the Swift and Objective-C interoperability. In my Objective-C class the methods with parameters of my Swift class are not recognized. I have integrated the Swift class with the Bridging-Header correctly. Furthermore the Swift class inherits from NSObject and the variables and methods are declared with @objc. I have also tried @objcmembers... I can't find a solution to my problem on the internet. I am really desperate. My Swift class uses classes from other projects, which are also declared with @objc. Below is an example of my problem:
Project A: SwiftClassA
public class SwiftClassA: NSObject {
@objc public init() {
// do something
}
@objc public func aFunction() {
// do something
}
}
Project B: SwiftClassB
import ProjectA
public class SwiftClassB: NSObject {
@objc public init(testA: Double, testB: Double) {
// do something
}
@objc public func bFunction() {
let classA = SwiftClassA()
// do something
}
}
Project C: SwiftClassC
import ProjectA
import ProjectB
public class SwiftClassC: NSObject {
@objc public var classA: SwiftClassA
@objc public init(classA: SwiftClassA) {
self.classA = classA
}
@objc public func cFunction(testA: Double, testB: Double) {
let classB = SwiftClassB(testA: testA, testB: testB)
// do something
}
@objc public func cFunction() {
// do something
return
}
}
Project C: ObjcClass
#import <ProjectA-Swift.h>
#import <ProjectC-Swift.h>
@property (nonatomic, strong) SwiftClassC *swiftClassC;
@implementation ObjcClass
- (ObjcClass *_Nonnull)init {
SwiftClassA *swiftClassA = [[SwiftClassA alloc] init];
_swiftClassC = [[SwiftClassC alloc] initWithClassA: swiftClassA];
return self;
}
- (void)objcFunction {
[_swiftClassC cFunctionWithTestA: 1.0 testB: 1.0]; // Property ‚FunctionWithTestA' not found on object of type ’SwiftClassC *’
[_swiftClassC cFunction];
}
@end
@objcin front of everything. You can just put@objcMembersin front of your class declaration, like so:@objcMembers public class SwiftClassC: NSObject { // code }