<!--
{
  "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/insert(_:at:)-5dbx5",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMutableArray(im)insertObject:atIndex:"
  },
  "title" : "insert(_:at:)"
}
-->

# insert(_:at:)

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

```
func insert(_ anObject: Any, at index: Int)
```

## Parameters

`anObject`

The object to add to the array’s content. This value must not be `nil`.
  
  > Important:
  > Raises an `NSInvalidArgumentException` if `anObject` is `nil`.

`index`

The index in the array at which to insert `anObject`. This value must not be greater than the count of elements in the array.
  
  > Important:
  > Raises an `NSRangeException` if `index` is greater than the number of elements in the array.

## Discussion

If `index` is already occupied, the objects at `index` and beyond are shifted by adding `1` to their indices to make room.

Note that `NSArray` objects are not like C arrays. That is, even though you specify a size when you create an array, the specified size is regarded as a “hint”; the actual size of the array is still 0. This means that you cannot insert an object at an index greater than the current count of an array. For example, if an array contains two objects, its size is 2, so you can add objects at indices 0, 1, or 2. Index 3 is illegal and out of bounds; if you try to add an object at index 3 (when the size of the array is 2), `NSMutableArray` raises an exception.

## See Also

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

Removes the object at `index` .



---

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)