---
title: "init(forWritingToURL:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/filehandle/init(forwritingtourl:)"
---

# init(forWritingToURL:)

Returns a file handle initialized for writing to the file, device, or named socket at the specified URL.

## Declaration

```swift
convenience init(forWritingToURL url: URL) throws
```

## Parameters

- `url`: The URL of the file, device, or named socket to access.

## Return Value

Return Value The initialized file handle object or nil if no file exists at url.

## Discussion

Discussion The file pointer is set to the beginning of the file. The returned object responds only to write(_:). When using this method to create a file handle object, the file handle owns its associated file descriptor and is responsible for closing it. note: In Swift, this method returns a nonoptional result 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

### Related Documentation

- [write(_:)](foundation/filehandle/write(_:).md)

### Creating a file handle

- [init(fileDescriptor:)](foundation/filehandle/init(filedescriptor:).md)
- [init(fileDescriptor:closeOnDealloc:)](foundation/filehandle/init(filedescriptor:closeondealloc:).md)
- [init(forReadingAtPath:)](foundation/filehandle/init(forreadingatpath:).md)
- [init(forReadingFromURL:)](foundation/filehandle/init(forreadingfromurl:).md)
- [init(forWritingAtPath:)](foundation/filehandle/init(forwritingatpath:).md)
- [init(forUpdatingAtPath:)](foundation/filehandle/init(forupdatingatpath:).md)
- [init(forUpdatingURL:)](foundation/filehandle/init(forupdatingurl:).md)
- [init(coder:)](foundation/filehandle/init(coder:).md)
