2016-07-31 43 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 .:看到更新的答案。 –