尝试使用 Swift 中的 unicode 字符定义自定义 NSCharacterSet
Trying to define a custom NSCharacterSet with unicode characters in Swift
我想在Swift中定义一个NSCharacterSet
包含:[A-Z](注意双字节,这不是[A-Z])。
执行此操作的正确语法是什么?我在 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.
您可以从 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"
// ...
我想在Swift中定义一个NSCharacterSet
包含:[A-Z](注意双字节,这不是[A-Z])。
执行此操作的正确语法是什么?我在 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.
您可以从 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"
// ...