I am trying to add swift 4 file in objective c project on Xcode 9. For that i am following these steps -
step 1: suppose swift class name is ShowAlertSwift.swift
import UIKit
@objc public class ShowAlertSwift: NSObject {
@objc func printSome() {
print("Print line System")
}
@objc func test () {
print("Test swift Class in objective C project")
}
}
step 2 : And the objective -c class first declare @class ShowAlertSwift
In objectiveClass.h I have followed these step
@class ShowAlertSwift
@interface ShowAlertSwift: NSObject
-(void)test;
-(void)printSome;
@end
@interface ViewController : UIViewController
@end
step 3 - In objectiveClass.m
#import "myproject-swift.h"
@interface UIViewController ()
{
}
@implementation
- (void)viewDidLoad
{
ShowAlertSwift *alertObj = [[ShowAlertSwift alloc]init];
[alertObj test];
}
I also set the following properties
Defines modules - Yes
Embedded_Content_Contains_Swift - Yes
Install Objective-C compatibility header - Yes
Objective C bridging header - $(SRCROOT)/Sources/SwiftBridging.h
Product Module Name - projectName
After all these stuff I am getting errors like
1. myproject-swift.h file not found
2. Consecutive statement on a line must be seprated by ';'
3. failed to emit precompiled header '/Users/xyz/pqr/Trunk/build/SharedPrecompiledHeaders/myproject-Bridging-Header-swift_1CBSOZ0HA1Z9R-clang_UESSBOJR5CSH.pch' for bridging header '/Users/xyz/pqr/Trunk/OpenPage2/com/myProject/login/Login_iPad/myProject-Bridging-Header.h'
Can any one has any idea how to resolve the issue on Xcode 9 in Swift 4 ?
I have done lots of thing to resolve this issue but I was not getting success. But Due to my dedication finally i solved this issue -
Note : To access swift class in objective C code you have to use the @objec keyword along the class statement like that
@objc public class ShowAlertSwift: NSObject
First create an swift class
import UIKit
@objc public class ShowAlertSwift: NSObject {
@objc func printSome() {
print("Print line System")
}
@objc func test () {
print("Test swift Class in objective C project")
}
}
In objectiveClass.h I have followed these step
@class ShowAlertSwift
@interface ViewController : UIViewController
@end
here there is no need to define an interface again as this interface is already exist in "myproject-swift.h" file. Refer question First to understand clearly or see below
@interface ShowAlertSwift: NSObject
-(void)test;
-(void)printSome;
@end
step 3 - In objectiveClass.m
#import "myproject-swift.h"
@interface UIViewController ()
{
}
@implementation
- (void)viewDidLoad
{
ShowAlertSwift *alertObj = [[ShowAlertSwift alloc]init];
[alertObj test];
}
Also enable/update these properties in your build settings -
1. Defines Module set to YES (Mandatory on project - not on target)
2. Embedded_Content_Contains_Swift - Yes
3. Install Objective-C compatibility header - Yes
4. Objective C bridging header - $(SRCROOT)/Sources/SwiftBridging.h
5. Product Module Name - projectName
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