Search code examples
objective-cswiftbridging-headerobjc-bridging-header

NS_ENUM object not compile after added in bridging header file


I have one enum which is in constant.h file ( Objective-C )

typedef NS_ENUM (NSInteger, EEFieldType) {
  EEFieldTypeHighFields = 1,
  EEFieldTypeMediumFields = 2,
  EEFieldTypeLowFields = 3
};

I have one bridging file which is connect to swift code and importing one file which file name is profile.

(ModuleName-Bridging-Header.h)

#import "Profile.h" 

profile file using below method which is not compiled in code.

- (EEFieldType)fieldTypeByPFType;

Error : Expected a type on EEFieldType.


Solution

  • [Answer moved and expanded from comments]

    With the additional information on the use of a pre-compiled header file (.pch) added in the comments, your problem comes down to Swift not using .pch files – they are an Objective-C compiler feature.

    In Objective.c Profile.h compiles as the header it depends on, constant.h, is imported by the .pch.

    In Swift Profile.h produces the missing type error as it does not import constant.h which defines the type.

    Simply import constant.h in Profile.h.

    Note: Doing this not only works for Swift it continues to work correctly for Objective-C – the .pch feature is a compiler option to speed up header processing and the Objective-C compiler will continue to use it, when it sees the constant.h import in Profile.h it will simple skip it as already imported by the .pch.