---
title: NSCharacterSet
framework: foundation
role: symbol
role_heading: Class
path: foundation/nscharacterset
---

# NSCharacterSet

An object representing a fixed set of Unicode character values for use in search operations.

## Declaration

```swift
class NSCharacterSet
```

## Overview

Overview In Swift, this bridges to a CharacterSet; use NSCharacterSet when you need reference semantics or other Foundation-specific behavior. An NSCharacterSet object represents a set of Unicode-compliant characters. NSString and NSScanner objects use NSCharacterSet objects to group characters together for searching operations, so that they can find any of a particular set of characters during a search. The cluster’s two public classes, NSCharacterSet and NSMutableCharacterSet, declare the programmatic interface for static and dynamic character sets, respectively. The objects you create using these classes are referred to as character set objects (and when no confusion will result, merely as character sets). Because of the nature of class clusters, character set objects aren’t actual instances of the NSCharacterSet or NSMutableCharacterSet classes but of one of their private subclasses. Although a character set object’s class is private, its interface is public, as declared by these abstract superclasses, NSCharacterSet and NSMutableCharacterSet. The character set classes adopt the NSCopying and NSMutableCopying protocols, making it convenient to convert a character set of one type to the other. The NSCharacterSet class declares the programmatic interface for an object that manages a set of Unicode characters (see the NSString class cluster specification for information on Unicode). NSCharacterSet’s principal primitive method, characterIsMember(_:), provides the basis for all other instance methods in its interface. A subclass of NSCharacterSet needs only to implement this method, plus mutableCopy(with:), for proper behavior. For optimal performance, a subclass should also override bitmapRepresentation, which otherwise works by invoking characterIsMember(_:) for every possible Unicode value. NSCharacterSet is “toll-free bridged” with its Core Foundation counterpart, CFCharacterSet. See Toll-Free Bridging for more information on toll-free bridging. important: The Swift overlay to the Foundation framework provides the CharacterSet structure, which bridges to the NSCharacterSet class and its mutable subclass, NSMutableCharacterSet. For more information about value types, see Working with Foundation Types.

## Topics

### Getting Standard Character Sets

- [alphanumerics](foundation/nscharacterset/alphanumerics.md)
- [capitalizedLetters](foundation/nscharacterset/capitalizedletters.md)
- [controlCharacters](foundation/nscharacterset/controlcharacters.md)
- [decimalDigits](foundation/nscharacterset/decimaldigits.md)
- [decomposables](foundation/nscharacterset/decomposables.md)
- [illegalCharacters](foundation/nscharacterset/illegalcharacters.md)
- [letters](foundation/nscharacterset/letters.md)
- [lowercaseLetters](foundation/nscharacterset/lowercaseletters.md)
- [newlines](foundation/nscharacterset/newlines.md)
- [nonBaseCharacters](foundation/nscharacterset/nonbasecharacters.md)
- [punctuationCharacters](foundation/nscharacterset/punctuationcharacters.md)
- [symbols](foundation/nscharacterset/symbols.md)
- [uppercaseLetters](foundation/nscharacterset/uppercaseletters.md)
- [whitespacesAndNewlines](foundation/nscharacterset/whitespacesandnewlines.md)
- [whitespaces](foundation/nscharacterset/whitespaces.md)

### Getting Character Sets for URL Encoding

- [urlFragmentAllowed](foundation/nscharacterset/urlfragmentallowed.md)
- [urlHostAllowed](foundation/nscharacterset/urlhostallowed.md)
- [urlPasswordAllowed](foundation/nscharacterset/urlpasswordallowed.md)
- [urlPathAllowed](foundation/nscharacterset/urlpathallowed.md)
- [urlQueryAllowed](foundation/nscharacterset/urlqueryallowed.md)
- [urlUserAllowed](foundation/nscharacterset/urluserallowed.md)

### Creating a Custom Character Set

- [init(coder:)](foundation/nscharacterset/init(coder:).md)
- [init(charactersIn:)](foundation/nscharacterset/init(charactersin:).md)
- [init(range:)](foundation/nscharacterset/init(range:).md)
- [NSOpenStepUnicodeReservedBase](foundation/1560803-nsopenstepunicodereservedbase.md)

### Creating and Managing Character Sets as Bitmap Representations

- [init(bitmapRepresentation:)](foundation/nscharacterset/init(bitmaprepresentation:).md)
- [init(contentsOfFile:)](foundation/nscharacterset/init(contentsoffile:).md)
- [bitmapRepresentation](foundation/nscharacterset/bitmaprepresentation.md)

### Inverting a Character Set

- [inverted](foundation/nscharacterset/inverted.md)

### Testing Set Membership

- [characterIsMember(_:)](foundation/nscharacterset/characterismember(_:).md)
- [hasMemberInPlane(_:)](foundation/nscharacterset/hasmemberinplane(_:).md)
- [isSuperset(of:)](foundation/nscharacterset/issuperset(of:).md)
- [longCharacterIsMember(_:)](foundation/nscharacterset/longcharacterismember(_:).md)

### Initializers

- [init(charactersInString:)](foundation/nscharacterset/init(charactersinstring:).md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Inherited By

- [NSMutableCharacterSet](foundation/nsmutablecharacterset.md)

### Conforms To

- [CVarArg](swift/cvararg.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSCopying](foundation/nscopying.md)
- [NSMutableCopying](foundation/nsmutablecopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSSecureCoding](foundation/nssecurecoding.md)
