5

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 ?

Shubham JAin
  • 593
  • 8
  • 15
  • `#import "myproject-Swift.h"`, where the word _Swift_ prefixes capitol `S` ([source](https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html)) – holex Oct 03 '17 at 13:45

3 Answers3

5

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
Shubham JAin
  • 593
  • 8
  • 15
0

Have you delete derived data ? if not then delete derived data and then clean+build your app .

This was help me in my code . So hope you will not get error.

Raj Oriya
  • 78
  • 4
  • 1
    I have deleted the derived data , clean and build but still getting the same errors which i mentioned above. 1. myproject-swift.h file not found 2. Consecutive statement on a line must be seprated by ';' – Shubham JAin Oct 03 '17 at 10:18
  • 1
    Go to header search path and delete all the code from there after that add new by pressing + icon and from xcode drag and drop the path of your ProjectName-Bridging-Header and try – Raj Oriya Oct 03 '17 at 10:25
0

I renamed the base folder of the project and that's why the problem is occurred.

So I had to rename each error line like

Restaurant-swift.h to FoodPicker_Restaurant-swift.h

the "-" is for the Space at the project name.