<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/FileHandle/init(fileDescriptor:closeOnDealloc:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSFileHandle(im)initWithFileDescriptor:closeOnDealloc:"
  },
  "title" : "init(fileDescriptor:closeOnDealloc:)"
}
-->

# init(fileDescriptor:closeOnDealloc:)

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

```
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

An initialized file handle object.

## Discussion

If `flag` is <doc://com.apple.documentation/documentation/Swift/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 <doc://com.apple.documentation/documentation/Swift/true> for the `flag` parameter.

## See Also

[`closeFile()`](/documentation/Foundation/FileHandle/closeFile())

Disallows further access to the represented file or communications channel and signals end of file on communications channels that permit writing.



---

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)