1

Let said I have Swift and Objc classes, and a MyProject-Bridging.h

BazSwiftClass.swift

public class BazSwiftClass: NSObject {
    public let foo: String
    @objc init(foo: String) {
        self.foo = foo
    }
}

FooClass.h

#import "MyProject-Swift.h"
@interface FooClass : NSObject {
     - (BazSwiftClass)bazMethod;
}

I want to access the FooClass.h in another Swift Class (let said BarSwiftClass)

public class BarSwiftClass {
     public hello() -> BazSwiftClass {
          return FooClass().bazMethod()
     } 
}

I need FooClass.h in my swift file, so I added FooClass in my MyProject-Bridging.h

#import "FooClass.h"

So the Flow is like BarSwiftClass -> FooClass -> BazSwiftClass

It can't work because the compiler show "failed to emit precompiled header" and I think it is because of #import "FooClass.h" in MyProejct-Bridging.h

Please help.

Thank you.

Jefferson Setiawan
  • 536
  • 1
  • 5
  • 22

1 Answers1

4

Replace your code

#import "MyProject-Swift.h"
@interface FooClass : NSObject {
     - (BazSwiftClass)bazMethod;
}

With

@class BazSwiftClass
@interface FooClass : NSObject {
     - (BazSwiftClass)bazMethod;
}

and in FooClass.m File you can import #import "MyProject-Swift.h"

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
  • 1
    Thank you @PrashantTukadiya !! After use (@)class BazSwiftClass I can make it compiled. – Jefferson Setiawan Jun 12 '18 at 08:44
  • In general import as few things as possible in your `.h` files and try to use only forward class and protocol references. – Alper Jun 12 '18 at 09:29
  • In general import as few things as possible in your `.h` files and try to use only forward class and protocol references. – Alper Jun 12 '18 at 09:29