---
title: "init(fileDescriptor:closeOnDealloc:)"
framework: foundation
role: symbol
role_heading: Initializer
path: "foundation/filehandle/init(filedescriptor:closeondealloc:)"
---

# init(fileDescriptor:closeOnDealloc:)

Creates and returns a file handle object associated with the specified file descriptor and deallocation policy.

## Declaration

```swift
init(fileDescriptor fd: Int32, closeOnDealloc closeopt: Bool)
```

## Parameters

- `fd`: The POSIX file descriptor with which to initialize the file handle.
- `closeopt`: doc://com.apple.documentation/documentation/Swift/true if the returned file handle object should take ownership of the file descriptor and close it for you or doc://com.apple.documentation/documentation/Swift/false if you want to maintain ownership of the file descriptor.

## Return Value

Return Value An initialized file handle object.

## Discussion

Discussion If flag is false, the file descriptor you pass in to this method isn’t owned by the file handle object. In such a case, you’re responsible for closing the file descriptor at some point after disposing of the file handle object. If you want the file handle object to close the descriptor for you automatically, pass true for the flag parameter.

## See Also

### Related Documentation

- [closeFile()](foundation/filehandle/closefile().md)

### Creating a file handle

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