斯威夫特3.0
几乎与Swift 2.0相同。OptionSetType重命名为OptionSet,并且按惯例将枚举写为小写。
struct MyOptions : OptionSet {
let rawValue: Int
static let firstOption = MyOptions(rawValue: 1 << 0)
static let secondOption = MyOptions(rawValue: 1 << 1)
static let thirdOption = MyOptions(rawValue: 1 << 2)
}
none
Swift 3建议不要提供选项,而只是使用空数组文字:
let noOptions: MyOptions = []
其他用法:
let singleOption = MyOptions.firstOption
let multipleOptions: MyOptions = [.firstOption, .secondOption]
if multipleOptions.contains(.secondOption) {
print("multipleOptions has SecondOption")
}
let allOptions = MyOptions(rawValue: 7)
if allOptions.contains(.thirdOption) {
print("allOptions has ThirdOption")
}
雨燕2.0
在Swift 2.0中,协议扩展会处理大多数样板文件,这些样板文件现在已作为符合的结构导入OptionSetType
。(RawOptionSetType
自Swift 2 beta 2起已消失。)声明要简单得多:
struct MyOptions : OptionSetType {
let rawValue: Int
static let None = MyOptions(rawValue: 0)
static let FirstOption = MyOptions(rawValue: 1 << 0)
static let SecondOption = MyOptions(rawValue: 1 << 1)
static let ThirdOption = MyOptions(rawValue: 1 << 2)
}
现在我们可以使用基于集合的语义MyOptions
:
let singleOption = MyOptions.FirstOption
let multipleOptions: MyOptions = [.FirstOption, .SecondOption]
if multipleOptions.contains(.SecondOption) {
print("multipleOptions has SecondOption")
}
let allOptions = MyOptions(rawValue: 7)
if allOptions.contains(.ThirdOption) {
print("allOptions has ThirdOption")
}
斯威夫特1.2
看着由夫特导入的(Objective-C的选项UIViewAutoresizing
,例如),我们可以看到,选项被声明为struct
符合协议RawOptionSetType
,这反过来又符合 _RawOptionSetType
,Equatable
,RawRepresentable
,BitwiseOperationsType
,和NilLiteralConvertible
。我们可以这样创建自己的:
struct MyOptions : RawOptionSetType {
typealias RawValue = UInt
private var value: UInt = 0
init(_ value: UInt) { self.value = value }
init(rawValue value: UInt) { self.value = value }
init(nilLiteral: ()) { self.value = 0 }
static var allZeros: MyOptions { return self(0) }
static func fromMask(raw: UInt) -> MyOptions { return self(raw) }
var rawValue: UInt { return self.value }
static var None: MyOptions { return self(0) }
static var FirstOption: MyOptions { return self(1 << 0) }
static var SecondOption: MyOptions { return self(1 << 1) }
static var ThirdOption: MyOptions { return self(1 << 2) }
}
现在,我们可以像对待MyOptions
苹果文档中所述的那样对待这个新的选项集:您可以使用enum
-like语法:
let opt1 = MyOptions.FirstOption
let opt2: MyOptions = .SecondOption
let opt3 = MyOptions(4)
而且它的行为也像我们期望的选项一样:
let singleOption = MyOptions.FirstOption
let multipleOptions: MyOptions = singleOption | .SecondOption
if multipleOptions & .SecondOption != nil { // see note
println("multipleOptions has SecondOption")
}
let allOptions = MyOptions.fromMask(7) // aka .fromMask(0b111)
if allOptions & .ThirdOption != nil {
println("allOptions has ThirdOption")
}
我已经构建了一个生成器来创建Swift选项集,而无需进行所有查找/替换。
最新:对Swift 1.1 beta 3的修改。
RawOptionsSetType
:nshipster.com/rawoptionsettype