C6Silver C6Silver - 4 months ago 10
Swift Question

Access Swift Class in Objective C Header

I have been mixing Swift and Objective C just fine but I'm having issues gaining access to a Swift class from the HEADER file of Objective C. I can do so successfully in the .m file.

Within the .h file I import the Xcode generated file that has the format "Appname-Swift.h". However, in doing so I get the message that the file is not found. I can do this same import in my .m file with no issue. However, I need it in the .h file as I reference a Swift class that I need access to with public API.

How can I make use of the Swift class from the .h portion of Objective C?

Example:

#import <UIKit/UIKit.h>
#import "MyApp-Swift.h"

@interface SelectedContactsVC : UIViewController

@property (nonatomic,strong) MapVC *mapVC;

@end


MapVC above is a Swift class.

Answer

Move #import "MyApp-Swift.h" to .m file.

And make your .h file as:

#import <UIKit/UIKit.h>
@class MapVC;

@interface SelectedContactsVC : UIViewController

@property (nonatomic,strong) MapVC *mapVC;

@end

Swift cannot generate "MyApp-Swift.h", if it's imported from Objective-C header, sort of mutual dependency thing maybe.

Comments