swift2 查看NSCharacterSet中的字符(Swift)[duplicate]

afdcj2ne  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(157)

此问题在此处已有答案

NSArray from NSCharacterSet(9个答案)
六年前就关门了。
当我试图像这样列出NSCharacterSet中的字符时

print(NSCharacterSet.URLQueryAllowedCharacterSet())

它不会输出字符,而是类似于
〈__NSCF字符集:0x1759b900的数据类型〉

vx6bjr1n

vx6bjr1n1#

基于another answer,以下是Swift 2.0/3.0中派生的更干净的版本:

extension NSCharacterSet {
    var characters:[String] {
        var chars = [String]()
        for plane:UInt8 in 0...16 {
            if self.hasMemberInPlane(plane) {
            let p0 = UInt32(plane) << 16
            let p1 = (UInt32(plane) + 1) << 16
            for c:UTF32Char in p0..<p1 {
                    if self.longCharacterIsMember(c) {
                        var c1 = c.littleEndian
                        let s = NSString(bytes: &c1, length: 4, encoding: String.Encoding.utf32LittleEndian.rawValue)!
                        chars.append(String(s))
                    }
                }
            }
        }
        return chars
    }
}

用法:

let charset = NSCharacterSet.URLQueryAllowedCharacterSet()
print(charset.characters.joinWithSeparator(""))

相关问题