一个开源代码LogFlag类(CocoaLumberjack.swift不再编译,因为在Xcode 6 Beta 7中RawOptionSetType已被更改为实现BitwiseOperationsType。我无法知道如何实现操作符函数。
下面的示例使用来自CocoaLumberjack.swift的LogFlag:
// 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 }
}
答案 0 :(得分:1)
添加
就足够了static var allZeros: LogFlag { return nil }
到结构定义。剩余的运营商&
,|
,^
和~
协议已被定义为通用函数,例如
func &<T : _RawOptionSetType>(a: T, b: T) -> T
如果您实际上想要覆盖运营商,请执行(作为示例)
func &(a: LogFlag, b: LogFlag) -> LogFlag {
return LogFlag(a.value & b.value)
}