Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See characters in an NSCharacterSet (Swift) [duplicate]

Tags:

swift

swift2

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>

like image 544
Desmond Hume Avatar asked Dec 15 '22 09:12

Desmond Hume


1 Answers

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(""))
like image 160
Desmond Hume Avatar answered Jan 22 '23 09:01

Desmond Hume