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.
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.
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>.
Finally I got solution. I did following changes. Here below is to do list in your Framework.
Swift classes usage in Objective C classes
#import <YourProjectName/YourProjectName-Swift.h>
in Objective
cI 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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With