<!--
{
  "availability" : [
    "iOS: 2.0.0 - 27.0.0",
    "iPadOS: 2.0.0 - 27.0.0",
    "macCatalyst: 13.1.0 - 27.0.0",
    "macOS: 10.0.0 - 27.0.0",
    "tvOS: 9.0.0 - 27.0.0",
    "visionOS: 1.0.0 - 27.0.0",
    "watchOS: 2.0.0 - 27.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSArray/write(toFile:atomically:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSArray(im)writeToFile:atomically:"
  },
  "title" : "write(toFile:atomically:)"
}
-->

# write(toFile:atomically:)

Writes the contents of the array to a file at a given path.

```
func write(toFile path: String, atomically useAuxiliaryFile: Bool) -> Bool
```

## Parameters

`path`

The path at which to write the contents of the array.

    If     `path`     contains a tilde (~) character, you must expand it with [`expandingTildeInPath`](/documentation/Foundation/NSString/expandingTildeInPath)     before invoking this method.

`useAuxiliaryFile`

If <doc://com.apple.documentation/documentation/Swift/true>, the array is written to an auxiliary file, and then the auxiliary file is renamed to `path`. If <doc://com.apple.documentation/documentation/Swift/false>, the array is written directly to `path`. The <doc://com.apple.documentation/documentation/Swift/true> option guarantees that `path`, if it exists at all, won’t be corrupted even if the system should crash during writing.

## Return Value

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

## Discussion

If the array’s contents are all property list objects (`NSString`, `NSData`, `NSArray`, or `NSDictionary` objects), the file written by this method can be used to initialize a new array with the class method [`arrayWithContentsOfFile:`](/documentation/Foundation/NSArray/arrayWithContentsOfFile:) or the instance method [`init(contentsOfFile:)`](/documentation/Foundation/NSArray/init(contentsOfFile:)). This method recursively validates that all the contained objects are property list objects before writing out the file, and returns <doc://com.apple.documentation/documentation/Swift/false> if all the objects are not property list objects, since the resultant file would not be a valid property list.

## See Also

[`init(contentsOfFile:)`](/documentation/Foundation/NSArray/init(contentsOfFile:))

Initializes a newly allocated array with the contents of 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)