Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Error Receiver for class message is a forward declaration

I'm working on creating my own framework, I added objective c files and its working fine after that I needed to add some swift file after adding swift file xcode is not giving me option of auto Creating Bridging then I followed Mix and Match approach. I followed everything which I thing I understand.

I want to access swift file into Objective c but I'm getting error of forward declaration so Here is sample code I attached, kindly guide me where I'm doing wrong.

TestObjectCFile.h

#import <Foundation/Foundation.h>
@class TestSwiftFile;
@interface TestObjCFile : NSObject


@end

TestObjectCFile.m

#import "TestObjCFile.h"
#import <TestFrameworkTry/TestFrameworkTry-Swift.h>
#import "TestFrameworkTry.h"


@implementation TestObjCFile

- (void)TestMethodForImportingSwiftFile
{

    TestSwiftFile * testSwiftFile = [[TestSwiftFile alloc] init];
//    TestSwiftFile * testSwiftFile = [self returnSwiftClassInstance];
    NSLog(@"%@",testSwiftFile);
}
@end

TestSwiftFile.swift

import Foundation

And the error occur on TestObjectCFile.m following line.

TestSwiftFile * testSwiftFile = [[TestSwiftFile alloc] init];

as show attached picture. enter image description here

like image 313
Aleem Avatar asked Jan 23 '17 07:01

Aleem


People also ask

Is a forward declaration?

A forward declaration tells the compiler about the existence of an entity before actually defining the entity. Forward declarations can also be used with other entity in C++, such as functions, variables and user-defined types.

What is forward declaration in Objective C?

In Objective-C, classes and protocols can be forward-declared like this: @class MyClass; @protocol MyProtocol; In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. MyClass * or id<MyProtocol>.


1 Answers

Finally I got solution. I did following changes. Here below is to do list in your Framework.

Swift classes usage in Objective C classes

  • Use Open Keyword before class name in Swift
  • Use @objc Keyword before Open Keyword of Swift class
  • Import all header of objective c classes in Umbrella file YourProject.h those are consuming Swift Classes.
  • Use #import <YourProjectName/YourProjectName-Swift.h> in Objective c

I followed Apple's Mix and Match approach.

Note: sometimes the <YourProjectName/YourProjectName-Swift.h> file can not be imported into headers, but only into .m files. FYI, this file is created automatically by the compiler. If you search your project for it, you won't be able to find it, but if you ⌘ click on it (as a file imported in your code) in a file that is in your target, Xcode will (should) open it and show you all the Objective-C class interfaces for your Swift classes. To see the name for the Swift -> Objective-C module header, go to your target's Build Settings and search for $(SWIFT_MODULE_NAME)-Swift.h or simply -Swift.

like image 74
Aleem Avatar answered Oct 17 '22 17:10

Aleem