<!--
{
  "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/replaceObject(at:with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMutableArray(im)replaceObjectAtIndex:withObject:"
  },
  "title" : "replaceObject(at:with:)"
}
-->

# replaceObject(at:with:)

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

```
func replaceObject(at index: Int, with anObject: Any)
```

## Parameters

`index`

The index of the object to be replaced. This value must not exceed the bounds of the array.
  
  > Important:
  > Raises an `NSRangeException` if `index` is beyond the end of the array.

`anObject`

The object with which to replace the object at index `index` in the array. This value must not be `nil`.
  
  > Important:
  > Raises an `NSInvalidArgumentException` if `anObject` is `nil`.

## See Also

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

Removes the objects at the specified indexes from the array.

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

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

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

Removes the object at `index` .

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

Inserts a given object into the array’s contents at a given 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)