Swift class in ObjC Project : "Receiver 'MySwift' for class message is a forward declaration" error even after adding @obj and open prefixes

After spending a few hours to integrate swift files successfully to my Objective-C based iOS project, I face the next challenge now :

  1. In one of my Objective-C class header (Say, MyObjCClass.h), I do the forward declaration for MySwiftClass.
  2. In the respective .m file, I define an instance of MySwiftClass _swiftClassInstance.
  3. In the init method of MyObjCClass, I try to instantiate it as follows :
_swiftClassInstance = [[MySwiftClass alloc] init];

When I compile the code, I get 2 errors :

  • Receiver 'SocketIOClient' for class message is a forward declaration
  • Receiver type 'SocketIOClient' for instance message is a forward declaration

I have already done following :

  1. Imported MyProject-Swift.h in the MyObjCClass.m file.
  2. Marked the swift class with @objc and open

So, what can be the reason for the issue?

Replies

>> In one of my Objective-C class header (Say, MyObjCClass.h), I do the forward declaration for MySwiftClass.


What specifically is that line of code?


>> Receiver 'SocketIOClient' for class message is a forward declaration


This is the actual name of the class that you referred to as MySwiftClass? It's OK to suppress the actual name for the purposes of posting here, but your description is a bit confusing because inconsistent.


>> Imported MyProject-Swift.h in the MyObjCClass.m file.


What is the actual content of this file in relation the MySwiftClass type? You don't have to show the members (properties and methods), just the @interface that declares the class, and anything else that's relevant.


Note that if MyProject-Swift.h contains an @interface for MySwiftClass, and it's #imported into MyObjCClass.m, then it's "impossible" that you should get a forward declaration error on the Obj-C side. The error suggests that there's something else wrong, such as a subtle typo in one of the declared class names, or that there are two MyProject-Swift.h files in different locations, or something like that.


Edit: Oh, one more thing I meant to ask. Is this an app target, or something else (like a framework)?

Thanks for the reply.


MyProject-Siwft.h is the bridging header from Swift to Objective-C which is autogenerated by Xcode. It includes all the header definitions for the swift classes, so that they can be used in the Objective-C code. So, does not define specific class interface.

I know, it's an old issue. But, to help other peoples I post my answer.

To create an instance of a swift you need to inherit of NSObject.

class MySwiftClass: NSObject {

}

Sometimes you need to add @objc component behind the class.

@objc class MySwiftClass: NSObject {

}