How to reference Swift enum in Objective-C Header

前端 未结 7 2070
星月不相逢
星月不相逢 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:09

    enums are one part of the swift Objective-C communication that does not always work. The thing is that in Objective-C, the enums can just be primitive types(NSInteger for example).

    In swift you can have enums on more types, for example like String. Which is one of the awesome things about swift.

    However, these enums wont be compiled to Objective-C because there is no equivalent for them. So they will simply be ignore and not generated in this header file that xcode creates for all your swift files. This is called Objective-C generated interface header:

    This generated file contains all the classes and enums available in objective-c . You have to mark everything you want to expose in Objective-c with @objc or make them public. You dont need any forward declaration for them to work, if you have this.

    Hope this helps you figure out why your enum is not visible in Objc. Let me know if it worked out.

提交回复
热议问题