In my Swift class, I have an OptionSetType
defined for fulfillment options.
struct FulfillmentOption : OptionSetType {
let rawValue: Int
static let Pickup = FulfillmentOption(rawValue: 1 << 0)
static let Shipping = FulfillmentOption(rawValue: 1 << 1)
static let UserShipping = FulfillmentOption(rawValue: 1 << 2)
}
I then create a variable to add/remove and read options. This works as expected.
var options: FulfillmentOption = []
options.insert(FulfillmentOption.Pickup)
options.contains(FulfillmentOption.Pickup)
However I need to access the options
variable from one of my Objective-C classes. Since OptionSetType
is not defined in Objective-C, the variable is not visible to any of my Objective-C classes.
What is the best way for me to expose this to Objective-C? Should I stop using OptionSetType
altogether?
I've considered doing creating public
and private
variables like this to convert between the two. I don't love this, but it's the best I've come up with thus far.
private var _options: FulfillmentOptions = []
private var options: UInt {
get {
// get raw value from _options
}
set {
// set raw value to _options
}
}
Is there a more elegant way to accomplish this? I'd like to avoid writing unnecessary code.
Not a direct answer to your question, but as an alternative you can work the other way around. Define
in an Objective-C header, this would be imported into Swift as
More Information can be found in the "Using Swift with Cocoa and Objective-C" reference: