Reputation: 431
I have to use an Objective-C static library in my Swift project. Anything else goes well but the NS_OPTIONS
enum defined in the Objective-C header file, like this:
#import <Foundation/Foundation.h>
typedef NS_OPTIONS(NSUInteger, MyOption) {
MyOptionNone = 0,
MyOptionTop = 1 << 0,
MyOptionLeft = 1 << 1,
MyOptionBottom = 1 << 2,
MyOptionRight = 1 << 3
};
@interface MyObjcClass : NSObject
@end
and then in the .swift file, how could I use this enum in a switch-case?
EDIT:
I use MyOption
in my Swift class:
let option1: MyOption = .Top
let option2: MyOption = .Bottom
let value = option1 & option2
then I get compile error:
Binary operator '&' cannot be applied to two `MyOption` operands
How to solve this problem?
Upvotes: 2
Views: 3254
Reputation: 385860
First, you need to fix your syntax. You need a semicolon after the closing brace:
typedef NS_OPTIONS(NSUInteger, MyOption) {
MyOptionNone = 0,
MyOptionTop = 1 << 0,
MyOptionLeft = 1 << 1,
MyOptionBottom = 1 << 2,
MyOptionRight = 1 << 3
};
Next, in your bridging header, you need to import the header file that defines MyOption
. When you first create a Swift source file to an Objective-C project, or when you first create an Objective-C source file in a Swift project, Xcode offers to create the bridging header for you. It's named ProjectName-Bridging-Header.h
. So for example:
Once you've done this, and both header files can be compiled without errors, you can use MyOption
from Swift. It's an OptionSetType
. Example:
Upvotes: 11