-1
我正在創建一個類,它使用swift的optionsettype協議,我從目標C枚舉轉換。我看到它們如何有助於提供有關操作或對象狀態的信息,例如操縱桿或滾動的方向。我無法圍繞如何將某些按位運算符轉換爲更快捷的方式困難。使用OptionSetTypes,將如何使用和賦值(&=)或或賦值(| =)?
從OptionSetType協議文件:
struct PackagingOptions : OptionSetType {
let rawValue: Int
init(rawValue: Int) { self.rawValue = rawValue }
static let Box = PackagingOptions(rawValue: 1)
static let Carton = PackagingOptions(rawValue: 2)
static let Bag = PackagingOptions(rawValue: 4)
static let Satchel = PackagingOptions(rawValue: 8)
static let BoxOrBag: PackagingOptions = [Box, Bag]
static let BoxOrCartonOrBag: PackagingOptions = [Box, Carton, Bag]
}
var packagingOptions: PackingOptions = .Box
if packingOptions == .Box {
packingOptions |= .Satchel
}
會是翻譯,而不是下面?
packaingOptions = [PackagingOptions,.Satchel]
相信具有如按位或分配相同的效果。上述情況下的工作和分配如何工作?
有關完整的討論,請參閱我的在線圖書:http://www.apeth.com/swiftBook/ch04.html#SECoptionsSets – matt