In my Swift class, I have an OptionSetType defined for fulfillment options.
在我的Swift类中,我有一个为履行选项定义的选项。
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.
但是,我需要从一个Objective-C类访问options变量。由于OptionSetType在Objective-C中没有定义,所以我的任何Objective-C类都看不到这个变量。
What is the best way for me to expose this to Objective-C? Should I stop using OptionSetType altogether?
对我来说,将其暴露给Objective-C的最佳方式是什么?我应该完全停止使用OptionSetType吗?
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.
有没有更优雅的方式来实现这一点?我想避免写不必要的代码。
1 个解决方案
#1
8
Not a direct answer to your question, but as an alternative you can work the other way around. Define
不是直接回答你的问题,但作为替代,你可以用另一种方法。定义
typedef NS_OPTIONS(NSInteger, FulfillmentOption) {
FulfillmentOptionPickup = 1 << 0,
FulfillmentOptionShipping = 1 << 1,
FulfillmentOptionUserShipping = 1 << 2,
};
in an Objective-C header, this would be imported into Swift as
在Objective-C标题中,这将被导入Swift。
public struct FulfillmentOption : OptionSetType {
public init(rawValue: Int)
public static var Pickup: FulfillmentOption { get }
public static var Shipping: FulfillmentOption { get }
public static var UserShipping: FulfillmentOption { get }
}
More Information can be found in the "Using Swift with Cocoa and Objective-C" reference:
更多的信息可以在“使用Swift与Cocoa和Objective-C”中找到:
- "Interaction with C APIs":
- “与C api交互”:
Swift also imports C-style enumerations marked with the
NS_OPTIONS
macro as a Swift option set. Option sets behave similarly to imported enumerations by truncating their prefixes to option value names.Swift还导入以NS_OPTIONS宏作为Swift选项集标记的c风格枚举。
- "Swift and Objective-C in the Same Project":
- “Swift and Objective-C in the Same Project”:
You’ll have access to anything within a class or protocol that’s marked with the
@objc
attribute as long as it’s compatible with Objective-C. This excludes Swift-only features such as those listed here:只要@objc属性与Objective-C兼容,您就可以访问类或协议中的任何内容。这就排除了只有swift的特性,比如这里列出的:
- ...
- …
- Structures defined in Swift
- 结构中定义的迅速
- ...
- …
#1
8
Not a direct answer to your question, but as an alternative you can work the other way around. Define
不是直接回答你的问题,但作为替代,你可以用另一种方法。定义
typedef NS_OPTIONS(NSInteger, FulfillmentOption) {
FulfillmentOptionPickup = 1 << 0,
FulfillmentOptionShipping = 1 << 1,
FulfillmentOptionUserShipping = 1 << 2,
};
in an Objective-C header, this would be imported into Swift as
在Objective-C标题中,这将被导入Swift。
public struct FulfillmentOption : OptionSetType {
public init(rawValue: Int)
public static var Pickup: FulfillmentOption { get }
public static var Shipping: FulfillmentOption { get }
public static var UserShipping: FulfillmentOption { get }
}
More Information can be found in the "Using Swift with Cocoa and Objective-C" reference:
更多的信息可以在“使用Swift与Cocoa和Objective-C”中找到:
- "Interaction with C APIs":
- “与C api交互”:
Swift also imports C-style enumerations marked with the
NS_OPTIONS
macro as a Swift option set. Option sets behave similarly to imported enumerations by truncating their prefixes to option value names.Swift还导入以NS_OPTIONS宏作为Swift选项集标记的c风格枚举。
- "Swift and Objective-C in the Same Project":
- “Swift and Objective-C in the Same Project”:
You’ll have access to anything within a class or protocol that’s marked with the
@objc
attribute as long as it’s compatible with Objective-C. This excludes Swift-only features such as those listed here:只要@objc属性与Objective-C兼容,您就可以访问类或协议中的任何内容。这就排除了只有swift的特性,比如这里列出的:
- ...
- …
- Structures defined in Swift
- 结构中定义的迅速
- ...
- …