<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "swift: 4.0.0 -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "Dispatch",
  "identifier" : "/documentation/Dispatch/DispatchIO/init(type:path:oflag:mode:queue:cleanupHandler:)-50rb0",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Dispatch"
    ],
    "preciseIdentifier" : "s:So14OS_dispatch_ioC8DispatchE4type4path5oflag4mode5queue14cleanupHandlerABSgAbCE10StreamTypeO_SPys4Int8VGs5Int32Vs6UInt16VSo0a1_b1_I0CyAQctcfc"
  },
  "title" : "init(type:path:oflag:mode:queue:cleanupHandler:)"
}
-->

# init(type:path:oflag:mode:queue:cleanupHandler:)

Creates a new I/O channel that accesses the file at the specified path, potentially creating that file in the process.

```
convenience init?(type: DispatchIO.StreamType, path: UnsafePointer<Int8>, oflag: Int32, mode: mode_t, queue: DispatchQueue, cleanupHandler: @escaping (Int32) -> Void)
```

## Parameters

`type`

The access semantics for the channel. For a list of possible values, see [`DispatchIO.StreamType`](/documentation/Dispatch/DispatchIO/StreamType).

`path`

The absolute path of the file you want to open.

`oflag`

The flags to pass to `open(2)` when opening the file at the specified path.

`mode`

The mode flags to pass to `open(2)`. Specify `O_CREAT` to create the file at the specified path; otherwise, specify 0.

`queue`

The dispatch queue on which to perform work.

`cleanupHandler`

The handler to execute once the channel is closed. This block has no return value and takes the following parameter:

- error: An `errno` condition if creating or opening the channel failed; otherwise, the value is `0`.

## Discussion

This method opens the channel by passing the `path`, `oflag`, and mode parameters to the low-level `open(2)` function. The file descriptor returned by that function remains open and under system control until you close the channel, or until an error occurs that causes the channel to release the file descriptor. After closing the file descriptor, the channel executes the specified `cleanupHandler` block on `queue`.

---

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)