9

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.

1 Answer 1

18

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.

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

3 Comments

Thank you, this worked! Is adding the "@class" this way sort of like a forward declaration?
Yes, it teaches the compiler that the symbol is actually a class, but gives no other info. In most cases it's useful enough to avoid mutual dependency.
Awesome! exactly what I was looking for too. Thanks!

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.