Objective-C
NSMutableCharacterSet *_alnum = [NSMutableCharacterSet characterSetWithCharactersInString:@"_"];
[_alnum formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
Swift
let _alnum = NSMutableCharacterSet(charactersIn: "_")
_alnum.formUnion(with: .alphanumerics)
Another way would have been to make it mutable and add it.
Objective-C
NSMutableCharacterSet *characterSet = [NSMutableCharacterSet alphanumericCharacterSet];
[characterSet addCharactersInString:@"_"];
Swift
let characterSet = NSMutableCharacterSet.alphanumeric()
characterSet.addCharacters(in: "_")
You could verify it has been added (in a Playground) with:
characterSet.characterIsMember(UInt16(Character("^").unicodeScalars.first!.value)) // false
characterSet.characterIsMember(UInt16(Character("_").unicodeScalars.first!.value)) // true -- YAY!
characterSet.characterIsMember(UInt16(Character("`").unicodeScalars.first!.value)) // false
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