Besat Besat - 1 year ago 54
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 Source
@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
// ...

must be included from the bridging header file.

Remark: Class names should start with a capital letter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download