iOS Developer Library

Developer

Foundation Framework Reference NSMutableCharacterSet Class Reference

Options
Deployment Target:

On This Page
Language:

NSMutableCharacterSet

Inheritance


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later.

The NSMutableCharacterSet class declares the programmatic interface to objects that manage a modifiable set of Unicode characters. You can add or remove characters from a mutable character set as numeric values in NSRange structures or as character values in strings, combine character sets by union or intersection, and invert a character set.

Mutable character sets are less efficient to use than immutable character sets. If you don’t need to change a character set after creating it, create an immutable copy with copy and use that.

NSMutableCharacterSet defines no primitive methods. Subclasses must implement all methods declared by this class in addition to the primitives of NSCharacterSet. They must also implement mutableCopyWithZone:.

NSMutableCharacterSet is “toll-free bridged” with its Core Foundation counterpart, CFMutableCharacterSetRef. See Toll-Free Bridging for more information.

  • Adds to the receiver the characters whose Unicode values are in a given range.

    Declaration

    Swift

    func addCharactersInRange(_ aRange: NSRange)

    Objective-C

    - (void)addCharactersInRange:(NSRange)aRange

    Parameters

    aRange

    The range of characters to add.

    aRange.location is the value of the first character to add; aRange.location + aRange.length– 1 is the value of the last. If aRange.length is 0, this method has no effect.

    Discussion

    This code excerpt adds to a character set the lowercase English alphabetic characters:

    • NSMutableCharacterSet *aCharacterSet = [[NSMutableCharacterSet alloc] init];
    • NSRange lcEnglishRange;
    • lcEnglishRange.location = (unsigned int)'a';
    • lcEnglishRange.length = 26;
    • [aCharacterSet addCharactersInRange:lcEnglishRange];

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Removes from the receiver the characters whose Unicode values are in a given range.

    Declaration

    Swift

    func removeCharactersInRange(_ aRange: NSRange)

    Objective-C

    - (void)removeCharactersInRange:(NSRange)aRange

    Parameters

    aRange

    The range of characters to remove.

    aRange.location is the value of the first character to remove; aRange.location + aRange.length– 1 is the value of the last. If aRange.length is 0, this method has no effect.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Adds to the receiver the characters in a given string.

    Declaration

    Swift

    func addCharactersInString(_ aString: String)

    Objective-C

    - (void)addCharactersInString:(NSString *)aString

    Parameters

    aString

    The characters to add to the receiver.

    Discussion

    This method has no effect if aString is empty.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Removes from the receiver the characters in a given string.

    Declaration

    Swift

    func removeCharactersInString(_ aString: String)

    Objective-C

    - (void)removeCharactersInString:(NSString *)aString

    Parameters

    aString

    The characters to remove from the receiver.

    Discussion

    This method has no effect if aString is empty.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Replaces all the characters in the receiver with all the characters it didn’t previously contain.

    Declaration

    Swift

    func invert()

    Objective-C

    - (void)invert

    Discussion

    Inverting a mutable character set, whether by invert or by invertedSet, is much less efficient than inverting an immutable character set with invertedSet.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

    See Also

    – invertedSet (NSCharacterSet)