27

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

I don't see anything similar for enums.

7 Answers 7

35
+100

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.

6
  • 5
    Agree that no good answer. The codebase I'm working on is in flux between Swift and Objective C, so my solution here was to declare the enum in a (somewhat) appropriate objc file instead, until we're fully on Swift. It's much simpler to share enums between objc and swift that way, using the bridging header file.
    – Elise
    Jan 28, 2016 at 16:15
  • 10
    Forward declaring works : typedef SWIFT_ENUM(NSInteger, MyEnum);
    – amleszk
    Sep 3, 2017 at 12:56
  • 11
    Try typedef NS_ENUM(NSInteger, MyEnum); for forward declaration. Feb 8, 2019 at 10:10
  • 7
    Does anyone know why typedef NS_ENUM(NSInteger, MyEnum); is working for me, but typedef SWIFT_ENUM(NSInteger, MyEnum); isn't?
    – ArielSD
    Jan 8, 2020 at 21:12
  • 2
    @ArielSD it seems that SWIFT_ENUM requires a third argument now, something called extensibility Sep 3, 2021 at 13:47
19

typedef SWIFT_ENUM(NSInteger, MyEnum);

2
  • Worked for me perfectly.
    – PashaN
    Oct 16, 2017 at 17:50
  • 1
    This doesn't work anymore. Try typedef NS_ENUM(NSInteger, MyEnum); as suggested by @zh
    – Z S
    May 2, 2023 at 17:14
3

In Mixed OBJC - Swift projects the best way to have enums work in both types of classes will be to define the enum in an OBJC header and not in a swift class. swift enums can't be used in OBJC header files taken from the answer here

2

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: enter image description here

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.

1
  • 1
    In my swift project I have enum with custom type(assume Test(name, address)). Is there any way to expose those enums as cutom type/class to object c? Sep 4, 2020 at 6:38
0

As far as I can tell, raf's answer is still correct. The cleanest solution is to simply leave your enum in an Objective-C file until you no longer need to access it from an Objective-C header. I wanted to offer another possible workaround, though.

In my case I am gradually rewriting a large Objective-C project in Swift, and I don't mind an imperfect workaround if it allows me to rewrite more of my code in Swift. So I've settled on simply passing the enum to Objective-C as it's raw type, Int, or NSInteger to Objective-C. For example:

- (void)doSomethingWithType:(NSInteger)rawType {
    if (rawType == ExampleTypeWhatever) {
        // Do something
    } // etc...
}

When I call this method from Swift, all I have to do is pass the .rawValue, instead of the actual enum value:

objcObject.doSomething(withType: ExampleType.whatever.rawValue)

This keeps things fairly simple on both sides, and it'll be easy to update once doSomethingWithType: is eventually rewritten in Swift.

-2

I just use NSInteger as return type for both header and implementation files. And when you need to use the enum, get the enum by let yourEnum = YourEnum(rawValue: Int(enumNSInteger)). This works for me.

-3

You can access Swift enums in Objective-C, but only if the enum is declared with an @objc qualifier, which means that it has to be of type Int.

8
  • 2
    Right and I've done that. That won't make it available in the header tho.
    – Phil
    Nov 3, 2015 at 15:59
  • Your Obj-C header has to #include "<your-project-name>-Swift.h". Is that what you need?
    – NRitH
    Nov 3, 2015 at 18:26
  • 3
    You can't include the -Swift.h file in an Objective-C header file. You can only include it in an implementation file.
    – Phil
    Nov 3, 2015 at 21:58
  • "You can't include the -Swift.h file in an Objective-C header file. You can only include it in an implementation file." @Phil: Really? Why not?
    – matt
    Nov 3, 2015 at 22:09
  • 3
    Also, there is this on that page, "To avoid cyclical references, don’t import Swift code into an Objective-C header (.h) file. Instead, you can forward declare a Swift class or protocol to reference it in an Objective-C interface."
    – Phil
    Nov 3, 2015 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.