Crazy Sage
Crazy Sage

Reputation: 464

What is proper way to forward declare NS_OPTIONS in Objective-C\C++

I'm generating bridges from cpp to swift using cpp-objcpp-objc chain and I need to generate NS_OPTIONS. In ObjC I have it as follows:

#import <Foundation/Foundation.h>
typedef NS_OPTIONS(NSInteger, PhonebookPhoneOptions)
{
    PhonebookPhoneOptionsOpt1 = 1 << 0,
    PhonebookPhoneOptionsOpt2 = 1 << 1,
    PhonebookPhoneOptionsOpt3 = 1 << 2,
    PhonebookPhoneOptionsOpt4 = 1 << 3,
};

It is forward-declared in my another header

#import <Foundation/Foundation.h>
typedef NS_OPTIONS(NSInteger, PhonebookPhoneOptions);
//typedef NS_ENUM(NSInteger, PhonebookPhoneOptions);

@protocol PhonebookDataRefreshedCallback

- (void)onEvent:(PhonebookPhoneOptions)param;

When I use NS_OPTIONS forward declaration I'm getting following error: With NS_ENUM forward it is as folows:

subprojects/controller/service-sbis-phonebook/phonebook/djinni/objc/phonebook/PhonebookDataRefreshedCallback.h:5:53: error: unnamed enumeration must be a definition typedef NS_OPTIONS(NSInteger, PhonebookPhoneOptions);

subprojects/controller/service-sbis-phonebook/phonebook/djinni/objc/phonebook/PhonebookDataRefreshedCallback.h:5:9: error: declaration does not declare anything [-Werror,-Wmissing-declarations] typedef NS_OPTIONS(NSInteger, PhonebookPhoneOptions);

With NS_ENUM it is as follows:

subprojects/controller/service-sbis-phonebook/phonebook/djinni/objc/phonebook/PhonebookPhoneOptions.h:6:31: error: typedef redefinition with different types ('NSInteger' (aka 'long') vs 'enum PhonebookPhoneOptions') typedef NS_OPTIONS(NSInteger, PhonebookPhoneOptions)

subprojects/controller/service-sbis-phonebook/phonebook/djinni/objc/phonebook/PhonebookDataRefreshedCallback.h:5:28: note: previous definition is here typedef NS_ENUM(NSInteger, PhonebookPhoneOptions);

When it was used solely as structure field I got some combination of NS_ENUM\NS_OPTIONS in ObjC\ObjC++ that was working, but when I try to use it as a function argument - it doesn't compile in any of the variants. What is the correct way to do such forward declaration?

Upvotes: 1

Views: 294

Answers (1)

pmdj
pmdj

Reputation: 23438

I don't think there's a way to forward declare these sensibly. Can't you simply pack the definition into a header file with no other content so it can be #included from just about anywhere? So, have a header file that contains nothing other than your enum, then include/import that from anything that needs it without pulling in any transitive dependencies.

If your problem is that NS_ENUM/NS_OPTIONS isn't available in pure C++, or rather, that #include <Foundation/Foundation.h> does not work there, try CF_ENUM/CF_OPTIONS instead, which are equivalent and defined in <CoreFoundation/CFAvailability.h>, which can be used from any of the C/C++/ObjC/ObjC++.

If that doesn't help you, please update your question to explain in more detail why you are trying to forward declare your enum type. As in, what specific problem arises as a result of not forward declaring the type? (provide minimal repro code)

Upvotes: 0

Related Questions