How to use enum in iOS 8 Swift public interface API

I am trying to create a Swift environment used by other iOS 8 apps using Swift or Objective C.

In several of my public functions, I needed to use an enum. Of course Swift has enums, but they are not compatible with client applications using Objective C. So, I tried to use an Objective C type enum of the form:

typedef NS_ENUM(NSInteger, MyEnumType)
{
    MyEnumTypeGreen,
    MyEnumTypeBlue,
    MyEnumTypeYellow
};

      

To make this type of enum publicly available, I place this declaration in PublicEnums.h. To access my Swift code, I need this line in my framework project Bridging-Header.h:

#import "PublicEnums.h"

      

But then I get this error:

Include a non-modular header inside the 'MyFramework' frame module, which puts a line in the auto-generated file MyFramework-Swift.h:

#import "/Users/sourcepath.../ObjectiveC/MyFramework-Bridging-Header.h"

      

Isn't there a way to use some kind of enum in a Swift framework that can be used from an Objective C application?

+3


source to share


1 answer


I just tested this on a new project and everything works fine. I think your error is due to misuse of the bridge header.

I think you shouldn't be importing the bridge header using the import statement. It should be automatically added to your target build settings under the header keyword:

Objective-C Bridging Header ------     projectName/projectName-Bridging-Header.h

      

The raw name of this key SWIFT_OBJC_BRIDGING_HEADER

.



Test:

    let x = MyEnumType.Blue
    println("\(x.toRaw())") // 1

      

Note that in Xcode 6.1, the last line would use rawValue

.

+1


source







All Articles