<!--
{
  "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/archiveRootObject(_:toFile:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSArchiver(cm)archiveRootObject:toFile:"
  },
  "title" : "archiveRootObject(_:toFile:)"
}
-->

# 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.

```
class func archiveRootObject(_ rootObject: Any, toFile path: String) -> Bool
```

## Parameters

`rootObject`

The root object of the object graph to archive.

`path`

The location of the file into which to write the archive.

## Return Value

<doc://com.apple.documentation/documentation/Swift/true> if the archive was written successfully, otherwise <doc://com.apple.documentation/documentation/Swift/false>.

## Discussion

This convenience method invokes [`archivedData(withRootObject:)`](/documentation/Foundation/NSArchiver/archivedData(withRootObject:)) to get the encoded data, and then sends that data object the message [`write(toFile:atomically:)`](/documentation/Foundation/NSData/write(toFile:atomically:)), using `path` for the first argument and <doc://com.apple.documentation/documentation/Swift/true> for the second.

The archived data should be retrieved from the archive by an [`NSUnarchiver`](/documentation/Foundation/NSUnarchiver) object.

## See Also

[`-  writeToFile:atomically:`](/documentation/Foundation/NSData/write(toFile:atomically:))

Writes the data object’s bytes to the file specified by a given path.



---

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)