0
一個開放的源代碼LogFlag類(CocoaLumberjack.swift不再編譯,因爲在Xcode 6測試版7 RawOptionSetType已更改爲實現BitwiseOperationsType。我無法知道如何實現操作功能。如何實現運營商BitwiseOperationsType
下面實施例使用從LogFlag CocoaLumberjack.swift:
// RawOptionSetType implements BitwiseOperationsType, so LogFlag won't compile until it implements the operators there
struct LogFlag : RawOptionSetType, BooleanType {
private var value: Int32 = 0
init(_ value: Int32) { self.value = value }
var boolValue: Bool { return self.value != 0 }
func toRaw() -> Int32 { return self.value }
static func fromRaw(raw: Int32) -> LogFlag? { return self(raw) }
static func fromMask(raw: Int32) -> LogFlag { return self(raw) }
static func convertFromNilLiteral() -> LogFlag { return self(0) }
static var Error: LogFlag { return self(1 << 0) }
static var Warn: LogFlag { return self(1 << 1) }
static var Info: LogFlag { return self(1 << 2) }
static var Debug: LogFlag { return self(1 << 3) }
static var Verbose: LogFlag { return self(1 << 4) }
}
嘗試:
func &(_: LogFlag, _:LogFlag) -> LogFlag { // What goes here? }
這是協議:
protocol BitwiseOperationsType {
func &(_: Self, _: Self) -> Self
func |(_: Self, _: Self) -> Self
func ^(_: Self, _: Self) -> Self
prefix func ~(_: Self) -> Self
/// The identity value for "|" and "^", and the fixed point for "&".
///
/// ::
///
/// x | allZeros == x
/// x^allZeros == x
/// x & allZeros == allZeros
/// x & ~allZeros == x
///
class var allZeros: Self { get }
}
這做到了!任何想法爲什麼我不必實施操作員?另外,如果我想要如何讓運營商超載?試圖更多地瞭解語法。 – Boon 2014-09-03 11:07:59
@Boon:查看更新。 - (不可能錯過你試圖理解Swift的各個方面:) – 2014-09-03 11:13:10
你抓住了它。 :)爲什麼你所有的零返回零?無,因爲我明白是可選的。沒有權利?爲什麼當所有的零沒有設置返回一個可選的時候,所有的零都返回零?在按位操作中如何使用nil? (如x | allZeros = x的示例所示) – Boon 2014-09-03 11:19:22