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.

8
  • you need to import only .h files in bridging header and not .m files so remove reference to .m file clean and rebuild Commented Jun 12, 2018 at 8:09
  • I have import only in .h file, but it still causing the error. I edit my question to avoid confusion. Commented Jun 12, 2018 at 8:12
  • Did you check this link @JeffersonSetiawan stackoverflow.com/questions/46293028/… Commented Jun 12, 2018 at 8:13
  • Yes, I know the problem from that link (I use bridging and in the MyProject-bridging, I import that objc file too), but I want to know can I keep using this method? Commented Jun 12, 2018 at 8:21
  • 1
    Check the answer Commented Jun 12, 2018 at 8:28

1 Answer 1

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"

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you @PrashantTukadiya !! After use (@)class BazSwiftClass I can make it compiled.
In general import as few things as possible in your .h files and try to use only forward class and protocol references.
In general import as few things as possible in your .h files and try to use only forward class and protocol references.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.