Enums Interoperability between Objective-C and Swift

I am migrating parts of my project to Swift and find out that enums are not compatible.

Also any property and functions that use an enum are not subject to objective-c, because enumeration has no equivalence in objective-c.

Even if the enumeration is of type NSInteger, the enumeration is still not displayed.

How do I get around this compatibility issue while keeping the swift class as clean as possible?


source to share

1 answer

Swift 2: In Swift 2, you can now enumerate enums in Objective-C. Make the enum non-generic, inherit from a simple numeric type (for example Int

), and don't use bound values. It will then render in Objective-C with the name enum pre-deferred for cases, so it looks like an Objective-C enum.

edit: To make sure it shows up, mark it with @objc

. If there are any issues preventing automatic bridging, you will receive a compiler error.

Swift 1.x: The answer is that you can't, at least for the time being. If you want to interact with enums, you need to declare them in Objective-C.

As Nate says, use NS_ENUM

, and Swift will infer the definition as enum

and remove the prefixes from the values.



All Articles