Besat Besat - 2 months ago 15
Swift Question

Calling objective-c method from Swift error

I get an error when I try to call objective-c method from swift.
my objective-c .h class:

@class myType;
@interface myClass : NSObject
- (myType *)myMethod;


then I will create an object form myClass and try to call myMethod in swift (I have declared anObject of type myClass):

let a = anObject.myMethod();


but I get an error:
Value of type 'myClass' has no member 'myMethod'
If I change myType to something else, the error goes away. So it should be a problem of not recognizing myType in swift..

I appreciate any help

Answer
@class myType;

is only a "forward declaration" of that class. In order to call the

- (myType *)myMethod;

method from either Objective-C or Swift, the compiler needs to know the actual interface declaration of that class. So "myType.h" or whatever file contains

@interface myType : NSObject
// ...
@end

must be included from the bridging header file.

Remark: Class names should start with a capital letter.

Comments