NSMutableArray
A dynamic ordered collection of objects.
Declaration
class NSMutableArrayOverview
You can use this type in Swift instead of an Array variable in cases that require reference semantics.
The NSMutableArray 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.
NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray. See Toll-Free Bridging for more information.
Accessing Values Using Subscripting
In addition to the provided instance methods, such as replaceObject(at:with:), you can access NSArray values by their indexes using subscripting.
Subclassing Notes
There is typically little reason to subclass NSMutableArray. 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
NSMutableArraystores 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
NSMutableArray defines five primitive methods:
In a subclass, you must override all these methods. You must also override the primitive methods of the NSArray class.
Topics
Creating and Initializing a Mutable Array
Adding Objects
Removing Objects
removeAllObjects()removeLastObject()remove(_:)remove(_:in:)removeObject(at:)removeObjects(at:)removeObject(identicalTo:)removeObject(identicalTo:in:)removeObjects(fromIndices:numIndices:)removeObjects(in:)removeObjects(in:)
Replacing Objects
replaceObject(at:with:)replaceObjects(at:with:)replaceObjects(in:withObjectsFrom:range:)replaceObjects(in:withObjectsFrom:)setArray(_:)
Filtering Content
Rearranging Content
exchangeObject(at:withObjectAt:)sort(using:)sort(comparator:)sort(options:usingComparator:)sort(_:context:)sort(using:)