@objc closed enum in Swift possible?

In Xcode 11 I've created a binary xcframework written Swift. One of the public types that it defines is an enum, and it needs to be usable from Swift and Objecitve-C. So I added the @objc modifier like so:


@objc public enum MyEnum: Int {
    case aaa
    case bbb
}


I import the xcframework into a separate Swift project and have the following switch statement:


switch myEnum {
    case .aaa:
        break
    case .bbb:
        break
}


Unfortuntely compiling the switch statement produces this warning:


"Switch covers known cases, but 'MyEnum' may have additional unknown values."


If I were to define the enum in Objective-C instead of Swift, I would use NS_CLOSED_ENUM to ensure that there are no future cases, which would solve the Swift warning. But I don't know how to make a "closed enum" in Swift. Enums in Swift already are closed, but I think the @objc flag might be canceling that out in the interface.


Can this be solved? Thanks in advance.

Answered by OOPer in 385837022

You may want to try @frozen attribute , enable library evolution mode and add `@frozen` to your enum.

Accepted Answer

You may want to try @frozen attribute , enable library evolution mode and add `@frozen` to your enum.

That's exactly what I was looking for, thanks!

@objc closed enum in Swift possible?
 
 
Q