<!--
{
  "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",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSArchiver"
  },
  "title" : "NSArchiver"
}
-->

# NSArchiver

A coder that stores an object’s data to an archive.

```
class NSArchiver
```

## Overview

[`NSArchiver`](/documentation/Foundation/NSArchiver), a concrete subclass of [`NSCoder`](/documentation/Foundation/NSCoder), provides a way to encode objects into an architecture-independent format that can be stored in a file. When you archive a graph of objects, the class information and instance variables for each object are written to the archive. The companion class [`NSUnarchiver`](/documentation/Foundation/NSUnarchiver) decodes the data in an archive and creates a graph of objects equivalent to the original set.

[`NSArchiver`](/documentation/Foundation/NSArchiver) stores the archive data in a mutable data object ([`NSMutableData`](/documentation/Foundation/NSMutableData)). After encoding the objects, you can have the [`NSArchiver`](/documentation/Foundation/NSArchiver) object write this mutable data object immediately to a file, or you can retrieve the mutable data object for some other use.

In macOS 10.2 and later, [`NSArchiver`](/documentation/Foundation/NSArchiver) and [`NSUnarchiver`](/documentation/Foundation/NSUnarchiver) have been replaced by [`NSKeyedArchiver`](/documentation/Foundation/NSKeyedArchiver) and [`NSKeyedUnarchiver`](/documentation/Foundation/NSKeyedUnarchiver) respectively—see [Archives and Serializations Programming Guide](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Archiving/Archiving.html#//apple_ref/doc/uid/10000047i).

## Topics

### Initializing an NSArchiver

[`init(forWritingWith:)`](/documentation/Foundation/NSArchiver/init(forWritingWith:))

Returns an archiver, initialized to encode stream and version information into a given mutable data object.

### Archiving data

[`archivedData(withRootObject:)`](/documentation/Foundation/NSArchiver/archivedData(withRootObject:))

Returns a data object containing the encoded form of the object graph whose root object is given.

[`archiveRootObject(_:toFile:)`](/documentation/Foundation/NSArchiver/archiveRootObject(_:toFile:))

Creates a temporary instance of `NSArchiver` and archives an object graph by encoding it into a data object and writing the resulting data object to a specified file.

[`encodeRootObject(_:)`](/documentation/Foundation/NSArchiver/encodeRootObject(_:))

Archives a given object along with all the objects to which it is connected.

[`encodeConditionalObject(_:)`](/documentation/Foundation/NSArchiver/encodeConditionalObject(_:))

Conditionally archives a given object.

### Getting the archived data

[`archiverData`](/documentation/Foundation/NSArchiver/archiverData)

The receiver’s archive data.

### Substituting classes or objects

[`classNameEncoded(forTrueClassName:)`](/documentation/Foundation/NSArchiver/classNameEncoded(forTrueClassName:))

Returns the name of the class used to archive instances of the class with a given true name.

[`encodeClassName(_:intoClassName:)`](/documentation/Foundation/NSArchiver/encodeClassName(_:intoClassName:))

Encodes a substitute name for the class with a given true name.

[`replace(_:with:)`](/documentation/Foundation/NSArchiver/replace(_:with:))

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

### Constants

[Archiving Exception Names](/documentation/Foundation/archiving-exception-names)

Raised by `NSArchiver` if there are problems initializing or encoding.



---

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)