Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference Swift enum in Objective-C Header

Tags:

Is there a way to reference a Swift enum from an Objective-C header? If you want to see Swift classes in the Objective-C header you can use

@objc class Foo 

I don't see anything similar for enums.

like image 281
Phil Avatar asked Nov 02 '15 22:11

Phil


Video Answer


1 Answers

What you want to do is called forward declaration. To forward declare an enum you can do:

enum name; 

But since the compiler won't know the size of the enum, you will only be able to use it as a pointer in your header file. Even doing this might prove problematic if you use compiler flags like -pedantic.

So in short, there is no good way to do this. Your best bet is not to, and access the enum from your implementation (.m) file instead.

In your implementation file, #import your swift bridging header file, and, without knowing more details about your problem, you could add private properties that use your enum like this:

@interface MyObjCClassDefinedInTheHFile()     @property (nonatomic, assign) SomeSwiftEnum type; @end 

Hope this helps.

like image 127
raf Avatar answered Oct 02 '22 04:10

raf