Issues using C++ enum values in Swift

I'm having issues using C++ enums in Swift. Enum classes work great! But when I'm dealing with code that uses regular enums - things get weird.

Assuming an enum, defined in the same target as my Swift code, like:

namespace MyNameSpace {

enum DrawingTool {
    kNoTool,
    kPencilDrawingTool,
    kEraserDrawingTool
};

}

I can see the DrawingTool struct (e.g. MyNameSpace.DrawingTool is a known type). But I can't see any of the variables that are supposed to be defined that are analogous to the values. I can't find them in the current Swift module space, or MyNameSpace. kNoTool, MyNameSpace.kNoTool and even MyNameSpace.DrawingTool.kNoTool are not found.

Is there something basic I'm missing? The documentation doesn't include example usage from the Swift side. But I'm assuming that the global variables should be trivial to access.

This should still work:

namespace MyNameSpace {
    typedef NS_ENUM(int, DrawingTool) {
        kNoTool,
        kPencilDrawingTool,
        kEraserDrawingTool
    };
}
...
var e = MyNameSpace.DrawingTool.noTool
e = .pencilDrawingTool

Doesn’t enum class also work, if this is C++?

1 Like

It does! But this is an existing codebase that already has a defined API used elsewhere. enum class would be an improvement - but that change would break other consumers of the API. I'm trying to use Swift while avoiding that larger refactor.

1 Like

I’m sorry:

6 Likes

@Colin_Cornaby Thanks for reporting this issue. This is a known issue with enumerations in namespaces in Swift 5.9: Enum cases don't show up when enum is decomposed in an enum that corresponds to a namespace - Unable to import C++ enumerations · Issue #67604 · apple/swift · GitHub . The fix for this is present in the Swift 5.10 toolchain that will be released next year.

3 Likes

Thanks for letting me know! I'll keep an eye out for the updated toolchain by way of Swift.org or Xcode.