TAGS :Viewed: 7 - Published at: a few seconds ago

[ Import a file in bridging-header which imports Swift header ]

I keep running into an error telling me that Xcode failed to import bridging header. I notice this only happens when I import one specific class which imports my Swift header (ProjectName-swift.h).

Is it possible to import a class which imports the Swift header? If not, how do I use the class that imports the Swift header in my Swift classes while still being able to use the Swift code I need in the file?

Answer 1

Move the Swift header import from your class's header file to the implementation file for that class, and use @class to make a forward declaration of your Swift class.

For example, if you need to import the Swift header for a Swift class named MySwiftClass, then before your @interface in your header file, put the following line:

@class MySwiftClass;

That will allow your header to compile without importing the Swift header file directly. Your implementation will still need it, so that's why the import statement should be moved to the implementation file.

The same can be done if what you're trying to forward declare is a protocol. You just use the @protocol keyword.

Using forward declarations of classes or protocols is the Objective-C way to break circular dependencies even when only Objective-C code is being used. Sometimes it's even necessary to use a forward declaration for a class or protocol defined in the same header file if for example you have a protocol and class that both refer to each other.