Class

NSMutable​Array

The NSMutable​Array class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray.

Overview

NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutable​Array. See Toll-Free Bridging for more information.

Accessing Values Using Subscripting

In addition to the provided instance methods, such as replace​Object(at:​with:​), you can access NSArray values by their indexes using subscripting.

mutableArray[3] = "someValue"

Subclassing Notes

There is typically little reason to subclass NSMutable​Array. The class does well what it is designed to do—maintain a mutable, ordered collection of objects. But there are situations where a custom NSArray object might come in handy. Here are a few possibilities:

  • Changing how NSMutable​Array stores the elements of its collection. You might do this for performance reasons or for better compatibility with legacy code.

  • Acquiring more information about what is happening to the collection (for example, statistics gathering).

Methods to Override

NSMutable​Array defines five primitive methods:

In a subclass, you must override all these methods. You must also override the primitive methods of the NSArray class.

Symbols

Creating and Initializing a Mutable Array

init()

Initializes a newly allocated array.

init(capacity:​ Int)

Returns an array, initialized with enough memory to initially hold a given number of objects.

Adding Objects

func add(Any)

Inserts a given object at the end of the array.

func add​Objects(from:​ [Any])

Adds the objects contained in another given array to the end of the receiving array’s content.

func insert(Any, at:​ Int)

Inserts a given object into the array’s contents at a given index.

func insert([Any], at:​ Index​Set)

Inserts the objects in the provided array into the receiving array at the specified indexes.

Removing Objects

func remove​All​Objects()

Empties the array of all its elements.

func remove​Last​Object()

Removes the object with the highest-valued index in the array

func remove(Any)

Removes all occurrences in the array of a given object.

func remove(Any, in:​ NSRange)

Removes all occurrences within a specified range in the array of a given object.

func remove​Object(at:​ Int)

Removes the object at index .

func remove​Objects(at:​ Index​Set)

Removes the objects at the specified indexes from the array.

func remove​Object(identical​To:​ Any)

Removes all occurrences of a given object in the array.

func remove​Object(identical​To:​ Any, in:​ NSRange)

Removes all occurrences of an​Object within the specified range in the array.

func remove​Objects(in:​ [Any])

Removes from the receiving array the objects in another given array.

func remove​Objects(in:​ NSRange)

Removes from the array each of the objects within a given range.

Replacing Objects

func replace​Object(at:​ Int, with:​ Any)

Replaces the object at index with an​Object.

func replace​Objects(at:​ Index​Set, with:​ [Any])

Replaces the objects in the receiving array at locations specified with the objects from a given array.

func replace​Objects(in:​ NSRange, with​Objects​From:​ [Any], range:​ NSRange)

Replaces the objects in the receiving array specified by one given range with the objects in another array specified by another range.

func replace​Objects(in:​ NSRange, with​Objects​From:​ [Any])

Replaces the objects in the receiving array specified by a given range with all of the objects from a given array.

func set​Array([Any])

Sets the receiving array’s elements to those in another given array.

Filtering Content

func filter(using:​ NSPredicate)

Evaluates a given predicate against the array’s content and leaves only objects that match.

Rearranging Content

func exchange​Object(at:​ Int, with​Object​At:​ Int)

Exchanges the objects in the array at given indexes.

func sort(using:​ [NSSort​Descriptor])

Sorts the receiver using a given array of sort descriptors.

func sort(comparator:​ (Any, Any) -> Comparison​Result)

Sorts the receiver in ascending order using the comparison method specified by a given Comparator block.

func sort(options:​ NSSort​Options = [], using​Comparator:​ (Any, Any) -> Comparison​Result)

Sorts the receiver in ascending order using the specified options and the comparison method specified by a given Comparator block.

func sort((Any, Any, Unsafe​Mutable​Raw​Pointer?) -> Int, context:​ Unsafe​Mutable​Raw​Pointer?)

Sorts the receiver in ascending order as defined by the comparison function compare.

func sort(using:​ Selector)

Sorts the receiver in ascending order, as determined by the comparison method specified by a given selector.