<!--
{
  "availability" : [
    "iOS: 2.0.0 - 11.0.0",
    "iPadOS: 2.0.0 - 11.0.0",
    "macCatalyst: 13.1.0 - 13.1.0",
    "macOS: 10.0.0 - 10.13.0"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSArchiver/replace(_:with:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSArchiver(im)replaceObject:withObject:"
  },
  "title" : "replace(_:with:)"
}
-->

# replace(_:with:)

Causes the receiver to treat subsequent requests to encode a given object as though they were requests to encode another given object.

```
func replace(_ object: Any, with newObject: Any)
```

## Parameters

`object`

An object in the object graph being archived.

`newObject`

The object with which to replace `object` in the archive.

## Discussion

Both `object` and `newObject` must be valid objects.

---

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)