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

# replace(_:with:)

Causes the receiver to substitute one given object for another whenever the latter is extracted from the archive.

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

## Parameters

`object`

The archived object to replace.

`newObject`

The object with which to replace `object`.

## Discussion

`newObject` can be of a different class from object, and the class mappings set by [`classNameDecoded(forArchiveClassName:)`](/documentation/Foundation/NSUnarchiver/classNameDecoded(forArchiveClassName:)-swift.type.method) and [`decodeClassName(_:asClassName:)`](/documentation/Foundation/NSUnarchiver/decodeClassName(_:asClassName:)-swift.method) are ignored.

---

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)