<!--
{
  "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/NSKeyedArchiverDelegate/archiver(_:willEncode:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSKeyedArchiverDelegate(im)archiver:willEncodeObject:"
  },
  "title" : "archiver(_:willEncode:)"
}
-->

# archiver(_:willEncode:)

Informs the delegate that `object` is about to be encoded.

```
optional func archiver(_ archiver: NSKeyedArchiver, willEncode object: Any) -> Any?
```

## Parameters

`archiver`

The archiver that sent the message.

`object`

The object that is about to be encoded. This value is never `nil`.

## Return Value

Either `object` or a different object to be encoded in its stead. The delegate can also modify the coder state. If the delegate returns `nil`, `nil` is encoded.

## Discussion

This method is called after the original object may have replaced itself with <doc://com.apple.documentation/documentation/ObjectiveC/NSObject-swift.class/replacementObject(for:)-60vwc>:.

This method is called whether or not the object is being encoded conditionally.

This method is not called for an object once a replacement mapping has been set up for that object (either explicitly, or because the object has previously been encoded). This method is also not called when `nil` is about to be encoded.

---

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)