检查 NSUnderlineStyle BitMask 的值

Checking the value of an NSUnderlineStyle BitMask

我希望能够正确读取 Swift 中 NSUnderlineStyle 的值。 比看起来要复杂

首先,快速回顾一下。 NSUnderlineStyle 是具有以下值的枚举:

NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09,
NSUnderlinePatternSolid = 0x0000,

NSUnderlinePatternDot = 0x0100,
NSUnderlinePatternDash = 0x0200,
NSUnderlinePatternDashDot = 0x0300,
NSUnderlinePatternDashDotDot = 0x0400,

NSUnderlineByWord = 0x8000 

三组不同的选项。根据Apple's documentation,"style, pattern, and optionally by-word mask are OR'd together to produce the value for NSUnderlineStyleAttributeName and NSStrikethroughStyleAttributeName."

NSUnderlineStyle 没有 实现新的 OptionSet Protocol,这意味着值必须与整数按位运算相结合(很明显)。如果是这样,则可能会出现类似于以下内容的情况:

let operation: NSDragOperation = [.copy, .move]
if operation.contains(.copy) {
  // Yay!
}

但是,遗憾的是,这不起作用。

我的理解是,要正确检查带有数字的位标志,它是 (A & B == B) 如果 B 在 A 中。

但是,这似乎对 NSUnderlineStyle 不起作用,我不确定为什么。我不是位掩码专家。

这是演示问题的示例代码。

let style = NSUnderlineStyle.styleSingle.rawValue |
  NSUnderlineStyle.patternDashDotDot.rawValue |
  NSUnderlineStyle.byWord.rawValue

if style & NSUnderlineStyle.styleSingle.rawValue == NSUnderlineStyle.styleSingle.rawValue {
  NSLog("style single found")
}

if style & NSUnderlineStyle.styleDouble.rawValue == NSUnderlineStyle.styleDouble.rawValue {
  NSLog("style double found")
}

if style & NSUnderlineStyle.styleThick.rawValue == NSUnderlineStyle.styleThick.rawValue {
  NSLog("style thick found")
}

if style & NSUnderlineStyle.patternSolid.rawValue == NSUnderlineStyle.patternSolid.rawValue {
  NSLog("pattern solid found")
}

if style & NSUnderlineStyle.patternDash.rawValue == NSUnderlineStyle.patternDash.rawValue {
  NSLog("pattern dash found")
}

if style & NSUnderlineStyle.patternDot.rawValue == NSUnderlineStyle.patternDot.rawValue {
  NSLog("pattern dot found")
}

if style & NSUnderlineStyle.patternDashDot.rawValue == NSUnderlineStyle.patternDashDot.rawValue {
  NSLog("pattern dash dot found")
}

if style & NSUnderlineStyle.patternDashDotDot.rawValue == NSUnderlineStyle.patternDashDotDot.rawValue {
  NSLog("pattern dash dot dot found")
}

if style & NSUnderlineStyle.byWord.rawValue == NSUnderlineStyle.byWord.rawValue {
  NSLog("by word flag found")
}

这段代码应该产生:

style single found
pattern dash dot dot found
by word flag found

而是产生:

style single found
pattern solid found
pattern dash dot dot found
by word flag found

还有其他失败的情况。

如何正确阅读下划线样式?

如有任何帮助,我们将不胜感激。另外,如果有人对失败的原因有任何见解,那也太好了。

"style" 和 "pattern" 的枚举值不是位标志。例如,NSUnderlineStyleNoneNSUnderlinePatternSolid 两者的值为零,并且 NSUnderlineStyleSingleNSUnderlineStyleDouble 有点 (0x01) 共同点。

您必须提取与每个 (样式、模式、标志)对应的位,并将该值与可能的值进行比较 枚举值,每组分开。

不幸的是,Apple 没有为每个组提供位掩码(到目前为止 如我所见)所以我们要定义我们自己的。 样式值(0x00、0x01、0x02、0x09)都使用位 0...3, 和模式值(0x0、0x100、0x200、0x300、0x400)都使用 位 8...11。这表明以下定义:

let underlineStyleMask   = 0x000F
let underlinePatternMask = 0x0F00

然后可以用作

// Check style:
switch style & underlineStyleMask {
case NSUnderlineStyle.styleNone.rawValue:
    NSLog("no style")
case NSUnderlineStyle.styleSingle.rawValue:
    NSLog("single style")
case NSUnderlineStyle.styleDouble.rawValue:
    NSLog("double style")
case NSUnderlineStyle.styleThick.rawValue:
    NSLog("thick style")
default:
    NSLog("unknown style")
}

// Check pattern:
switch style & underlinePatternMask {
case NSUnderlineStyle.patternSolid.rawValue:
    NSLog("solid pattern")
case NSUnderlineStyle.patternDot.rawValue:
    NSLog("dot pattern")
case NSUnderlineStyle.patternDash.rawValue:
    NSLog("dash pattern")
case NSUnderlineStyle.patternDashDot.rawValue:
    NSLog("dash dot pattern")
case NSUnderlineStyle.patternDashDotDot.rawValue:
    NSLog("dash dot dot pattern")
default:
    NSLog("unknown pattern")
}

// Check flags:
if style & NSUnderlineStyle.byWord.rawValue != 0 {
    NSLog("by word flag")
}

但请注意,如果 Apple 在中添加更多定义,这可能会中断 未来,例如

NSUnderlineStyleStrange = 0x22

会被错误检测为 NSUnderlineStyleThick