2016-08-16 51 views
1

我想在Swift中定義一個包含[A-Z](注意雙字節,不是[A-Z])的NSCharacterSet嘗試在Swift中定義一個帶有Unicode字符的自定義NSCharacterSet

什麼是正確的語法來做到這一點?下面我在Objectiv C中工作的代碼似乎並不像Swift那麼容易。

NSRange alphaDoubleByteRange; 
    NSMutableCharacterSet *alphaDoubleByteLetters; 
    alphaDoubleByteRange.location = (unsigned int)[@"A" characterAtIndex:0]; 
    alphaDoubleByteRange.length = 26; 
    alphaDoubleByteLetters = [[NSMutableCharacterSet alloc] init]; 
    [alphaDoubleByteLetters formUnionWithCharacterSet:[NSCharacterSet characterSetWithRange:alphaDoubleByteRange]]; 
    // Now alphaDoubleByteLetters contains what I want. 

回答

1

可以創建一個字符從Unicode標值的範圍內設定:

let firstCode = Int("A".unicodeScalars.first!.value) 
let lastCode = Int("Z".unicodeScalars.first!.value) 
let alphaDoubleByteRange = NSRange(location: firstCode, length: lastCode - firstCode + 1) 
let alphaDoubleByteLetters = NSCharacterSet(range: alphaDoubleByteRange) 

可選地,在Unicode table查找的字符,並使用 直接標量值:

let firstCode = 0xFF21 // "A" 
let lastCode = 0xFF3A // "Z" 
// ... 
+0

有趣!那是有效的。謝謝。 – Michel