【问题标题】:Trying to define a custom NSCharacterSet with unicode characters in Swift尝试在 Swift 中使用 unicode 字符定义自定义 NSCharacterSet
【发布时间】:2016-08-16 07:37:22
【问题描述】:

我想在 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.

【问题讨论】:

    标签: ios swift nscharacterset


    【解决方案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"
    // ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-08
      • 2013-09-22
      • 2020-06-20
      • 1970-01-01
      • 2012-10-10
      • 2019-11-23
      • 1970-01-01
      • 2015-11-10
      相关资源
      最近更新 更多