4

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

4
  • @class myType; is not sufficient, Swift needs to know the interface of that class. Commented Sep 13, 2016 at 18:46
  • Would you please give me an example? Commented Sep 13, 2016 at 18:47
  • The header file containing @interface myType .... @end must be included from the bridging header file. Commented Sep 13, 2016 at 18:49
  • Thanks... I added the myType header reference to my bridge class and it works. I will mark your answer as correct if you add it as an answer Commented Sep 13, 2016 at 18:56

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Having the same problem, cleaned derived data, quit my xcode and still same error. In my case i can see in that my swift calls calling the objc method is present in the import module but still does not build : Value of type 'MyPrint' has no member 'createLayout'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.