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>
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: String.Encoding.utf32LittleEndian.rawValue)!
chars.append(String(s))
}
}
}
}
return chars
}
}
Usage:
let charset = NSCharacterSet.URLQueryAllowedCharacterSet()
print(charset.characters.joinWithSeparator(""))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With