How to reference Swift enum in Objective-C Header

前端 未结 7 2067
星月不相逢
星月不相逢 2021-01-31 19:38

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
         


        
7条回答
  •  孤街浪徒
    2021-01-31 20:05

    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.

提交回复
热议问题