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

# write(to:options:)

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

## Declaration

```swift
func write(to url: URL, options writeOptionsMask: NSData.WritingOptions = []) throws
```

## Parameters

- `url`: The location to which to write the receiver’s bytes.
- `writeOptionsMask`: A mask that specifies options for writing the data. Constant components are described in doc://com.apple.foundation/documentation/Foundation/NSData/WritingOptions.

## Discussion

Discussion Since at present only file:// URLs are supported, there is no difference between this method and write(toFile:options:), 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, seeSecuring File Operations in Secure Coding Guide. note: In Swift, this method returns Void and is marked with the throws keyword to indicate that it throws an error in cases of failure. You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language and About Imported Cocoa Error Parameters.

## 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:atomically:)](foundation/nsdata/write(to:atomically:).md)
- [NSData.WritingOptions](foundation/nsdata/writingoptions.md)
