---
title: "write(to:atomically:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsdata/write(to:atomically:)"
---

# write(to:atomically:)

Writes the data object’s bytes to the location specified by a given URL.

## Declaration

```swift
func write(to url: URL, atomically: Bool) -> Bool
```

## Parameters

- `url`: The location to which to write the receiver’s bytes. Only file:// URLs are supported.
- `atomically`: If doc://com.apple.documentation/documentation/Swift/true, the data is written to a backup location, and then—assuming no errors occur—the backup location is renamed to the name specified by aURL; otherwise, the data is written directly to aURL. atomically is ignored if aURL is not of a type the supports atomic writes.

## Return Value

Return Value true if the operation succeeds, otherwise false.

## Discussion

Discussion Since at present only file:// URLs are supported, there is no difference between this method and write(toFile:atomically:), except for the type of the first argument. This method may not be appropriate when writing to publicly accessible files. To securely write data to a public location, use FileHandle instead. For more information, see Securing File Operations in Secure Coding Guide.

## See Also

### Writing Data to a File

- [write(toFile:atomically:)](foundation/nsdata/write(tofile:atomically:).md)
- [write(toFile:options:)](foundation/nsdata/write(tofile:options:).md)
- [write(to:options:)](foundation/nsdata/write(to:options:).md)
- [NSData.WritingOptions](foundation/nsdata/writingoptions.md)
