Besat
Besat

Reputation: 1438

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

Upvotes: 4

Views: 1850

Answers (2)

Besat
Besat

Reputation: 1438

turned out the real problem for me was something else! I had to simply delete the derived data:

  • Window -> Projects -> Derived Data -> Delete
  • Clean Project
  • Quit Xcode
  • Open Xcode
  • Build Project

Apparently, using swift I have to these steps more often..

Upvotes: 0

Martin R
Martin R

Reputation: 540145

@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.

Upvotes: 5

Related Questions