本文介绍了查看NSCharacterSet中的字符(快速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我试图像这样在NSCharacterSet中列出字符时
When I'm trying to list characters in an NSCharacterSet like this
print(NSCharacterSet.URLQueryAllowedCharacterSet())
它不会打印出字符,而是类似于
it doesn't print out the characters but rather something similar to
< __ NSCFCharacterSet:0x1759b900>
<__NSCFCharacterSet: 0x1759b900>
推荐答案
基于另一个答案,这是派生的Swift 2.0/3.0中更清洁的版本:
Based on another answer, here is a derived cleaner version in 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: NSUTF32LittleEndianStringEncoding)!
chars.append(String(s))
}
}
}
}
return chars
}
}
用法:
let charset = NSCharacterSet.URLQueryAllowedCharacterSet()
print(charset.characters.joinWithSeparator(""))
这篇关于查看NSCharacterSet中的字符(快速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!