<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSMutableArray",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMutableArray"
  },
  "title" : "NSMutableArray"
}
-->

# NSMutableArray

A dynamic ordered collection of objects.

```
class NSMutableArray
```

## Overview

You can use this type in Swift instead of an <doc://com.apple.documentation/documentation/Swift/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`](/documentation/Foundation/NSArray).

NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, <doc://com.apple.documentation/documentation/CoreFoundation/CFMutableArray>. See [Toll-Free Bridging](https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2) for more information.

### Accessing Values Using Subscripting

In addition to the provided instance methods, such as [`replaceObject(at:with:)`](/documentation/Foundation/NSMutableArray/replaceObject(at:with:)), you can access `NSArray` values by their indexes using *subscripting*.

```objc
mutableArray[3] = @"someValue";
```

### 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 `NSMutableArray` 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

`NSMutableArray` defines five primitive methods:

- [`insert(_:at:)`](/documentation/Foundation/NSMutableArray/insert(_:at:)-5dbx5)
- [`removeObject(at:)`](/documentation/Foundation/NSMutableArray/removeObject(at:))
- [`add(_:)`](/documentation/Foundation/NSMutableArray/add(_:))
- [`removeLastObject()`](/documentation/Foundation/NSMutableArray/removeLastObject())
- [`replaceObject(at:with:)`](/documentation/Foundation/NSMutableArray/replaceObject(at:with:))

In a subclass, you must override all these methods. You must also override the primitive methods of the [`NSArray`](/documentation/Foundation/NSArray) class.

## Topics

### Creating and Initializing a Mutable Array

[`arrayWithCapacity:`](/documentation/Foundation/NSMutableArray/arrayWithCapacity:)

Creates and returns an `NSMutableArray` object with enough allocated memory to initially hold a given number of objects.

[`arrayWithContentsOfFile:`](/documentation/Foundation/NSMutableArray/arrayWithContentsOfFile:)

Creates and returns a mutable array containing the contents of the file specified by the given path.

[`init(contentsOfURL:)`](/documentation/Foundation/NSMutableArray/init(contentsOfURL:))

Creates and returns a mutable array containing the contents specified by a given URL.

[`init()`](/documentation/Foundation/NSMutableArray/init())

Initializes a newly allocated array.

[`init(capacity:)`](/documentation/Foundation/NSMutableArray/init(capacity:))

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

[`initWithContentsOfFile:`](/documentation/Foundation/NSMutableArray/initWithContentsOfFile:)

Initializes a newly allocated mutable array with the contents of the file specified by a given path

[`initWithContentsOfURL:`](/documentation/Foundation/NSMutableArray/initWithContentsOfURL:)

Initialized a newly allocated mutable array with the contents of the location specified by a given URL.

### Adding Objects

[`add(_:)`](/documentation/Foundation/NSMutableArray/add(_:))

Inserts a given object at the end of the array.

[`addObjects(from:)`](/documentation/Foundation/NSMutableArray/addObjects(from:))

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

[`insert(_:at:)`](/documentation/Foundation/NSMutableArray/insert(_:at:)-5dbx5)

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

[`insert(_:at:)`](/documentation/Foundation/NSMutableArray/insert(_:at:)-73pln)

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

### Removing Objects

[`removeAllObjects()`](/documentation/Foundation/NSMutableArray/removeAllObjects())

Empties the array of all its elements.

[`removeLastObject()`](/documentation/Foundation/NSMutableArray/removeLastObject())

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

[`remove(_:)`](/documentation/Foundation/NSMutableArray/remove(_:))

Removes all occurrences in the array of a given object.

[`remove(_:in:)`](/documentation/Foundation/NSMutableArray/remove(_:in:))

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

[`removeObject(at:)`](/documentation/Foundation/NSMutableArray/removeObject(at:))

Removes the object at `index` .

[`removeObjects(at:)`](/documentation/Foundation/NSMutableArray/removeObjects(at:))

Removes the objects at the specified indexes from the array.

[`removeObject(identicalTo:)`](/documentation/Foundation/NSMutableArray/removeObject(identicalTo:))

Removes all occurrences of a given object in the array.

[`removeObject(identicalTo:in:)`](/documentation/Foundation/NSMutableArray/removeObject(identicalTo:in:))

Removes all occurrences of `anObject` within the specified range in the array.

[`removeObjects(fromIndices:numIndices:)`](/documentation/Foundation/NSMutableArray/removeObjects(fromIndices:numIndices:))

Removes the specified number of objects from the array, beginning at the specified index.

[`removeObjects(in:)`](/documentation/Foundation/NSMutableArray/removeObjects(in:)-4yb26)

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

[`removeObjects(in:)`](/documentation/Foundation/NSMutableArray/removeObjects(in:)-1udmn)

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

### Replacing Objects

[`replaceObject(at:with:)`](/documentation/Foundation/NSMutableArray/replaceObject(at:with:))

Replaces the object at `index` with `anObject`.

[`setObject:atIndexedSubscript:`](/documentation/Foundation/NSMutableArray/setObject:atIndexedSubscript:)

Replaces the object at the index with the new object, possibly adding the object.

[`replaceObjects(at:with:)`](/documentation/Foundation/NSMutableArray/replaceObjects(at:with:))

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

[`replaceObjects(in:withObjectsFrom:range:)`](/documentation/Foundation/NSMutableArray/replaceObjects(in:withObjectsFrom:range:))

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

[`replaceObjects(in:withObjectsFrom:)`](/documentation/Foundation/NSMutableArray/replaceObjects(in:withObjectsFrom:))

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

[`setArray(_:)`](/documentation/Foundation/NSMutableArray/setArray(_:))

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

### Filtering Content

[`filter(using:)`](/documentation/Foundation/NSMutableArray/filter(using:))

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

### Rearranging Content

[`exchangeObject(at:withObjectAt:)`](/documentation/Foundation/NSMutableArray/exchangeObject(at:withObjectAt:))

Exchanges the objects in the array at given indexes.

[`sort(using:)`](/documentation/Foundation/NSMutableArray/sort(using:)-4eh07)

Sorts the receiver using a given array of sort descriptors.

[`sort(comparator:)`](/documentation/Foundation/NSMutableArray/sort(comparator:))

Sorts the receiver in ascending order using the comparison method specified by a given [`Comparator`](/documentation/Foundation/Comparator) block.

[`sort(options:usingComparator:)`](/documentation/Foundation/NSMutableArray/sort(options:usingComparator:))

Sorts the receiver in ascending order using the specified options and the comparison method specified by a given [`Comparator`](/documentation/Foundation/Comparator) block.

[`sort(_:context:)`](/documentation/Foundation/NSMutableArray/sort(_:context:))

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

[`sort(using:)`](/documentation/Foundation/NSMutableArray/sort(using:)-537vs)

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



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)