2016-07-31 42 views
1

我想的值,以便能够正确读取斯威夫特的NSUnderlineStyle的值。 这似乎比看起来更复杂检查的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,“样式,图案,和任选地通过字掩码逻辑或运算,以产生用于NSUnderlineStyleAttributeNameNSStrikethroughStyleAttributeName值”。

NSUnderlineStyle 不会实现新的OptionSet Protocol,这意味着值必须与整数位操作(显然)结合使用。如果它这样做,类似于下面的东西是可能的:

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

但遗憾的是,这是行不通的。

我的理解是正确检查位标志与数字,它是(A &乙== 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 

还有其他一些情况下,这将失败。

我能做些什么,以适当读下划线样式?

任何帮助将不胜感激。此外,如果任何人有任何的洞察力,为什么这是失败的,那也将是了不起的。

为“风格”和“模式”

回答

2

枚举值不是位标志。例如,NSUnderlineStyleNoneNSUnderlinePatternSolid都具有零值,并且 NSUnderlineStyleSingleNSUnderlineStyleDouble具有共同位(0x01) 。

你必须提取对应于各组比特(样式,图案,标志),并比较该值针对可能 枚举值,分别为每个组。

不幸的是苹果并没有为每个组提供位掩码(据 我可以看到),所以我们定义我们自己。 样式值(0×00 0×01,0×02,×09)中的所有使用位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") 
} 

但要注意的是,如果苹果在 增加了更多的定义,未来这可能会断裂,例如

NSUnderlineStyleStrange = 0x22 

将错误地检测为NSUnderlineStyleThick

+0

嗨马丁,谢谢你解释一个例子!这工作很好。你能解释一下,你怎么知道用0x0F00和0x000F掩盖它?我希望有一天能达到这个水平,并且我想知道你的思考过程。 –

+1

@JosephE .:看到更新的答案。 –