<!--
{
  "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/NSMachPort",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMachPort"
  },
  "title" : "NSMachPort"
}
-->

# NSMachPort

A port that can be used as an endpoint for distributed object connections (or raw messaging).

```
class NSMachPort
```

## Overview

[`NSMachPort`](/documentation/Foundation/NSMachPort) is a subclass of [`Port`](/documentation/Foundation/Port) that wraps a Mach port, the fundamental communication port in macOS. [`NSMachPort`](/documentation/Foundation/NSMachPort) allows for local (on the same machine) communication only. A companion class, [`SocketPort`](/documentation/Foundation/SocketPort), allows for both local and remote distributed object communication, but may be more expensive than [`NSMachPort`](/documentation/Foundation/NSMachPort) for the local case.

To use [`NSMachPort`](/documentation/Foundation/NSMachPort) effectively, you should be familiar with Mach ports, port access rights, and Mach messages. See the Mach OS documentation for more information.

> Note:
> ``doc://com.apple.foundation/documentation/Foundation/NSMachPort`` conforms to the ``doc://com.apple.foundation/documentation/Foundation/NSCoding`` protocol, but only supports coding by an ``doc://com.apple.foundation/documentation/Foundation/NSPortCoder``. ``doc://com.apple.foundation/documentation/Foundation/Port`` and its subclasses do not support archiving.

## Topics

### Creating and Initializing

[`+  portWithMachPort:`](/documentation/Foundation/NSMachPort/port(withMachPort:))

Creates and returns a port object configured with the given Mach port.

[`+  portWithMachPort:options:`](/documentation/Foundation/NSMachPort/port(withMachPort:options:))

Creates and returns a port object configured with the specified options and the given Mach port.

[`-  initWithMachPort:`](/documentation/Foundation/NSMachPort/init(machPort:))

Initializes a newly allocated `NSMachPort` object with a given Mach port.

[`-  initWithMachPort:options:`](/documentation/Foundation/NSMachPort/init(machPort:options:))

Initializes a newly allocated `NSMachPort` object with a given Mach port and the specified options.

### Getting the Mach Port

[`machPort`](/documentation/Foundation/NSMachPort/machPort)

The Mach port used by the receiver, represented as an integer.

### Scheduling the Port on a Run Loop

[`-  removeFromRunLoop:forMode:`](/documentation/Foundation/NSMachPort/remove(from:forMode:))

Removes the receiver from the run loop mode `mode` of `runLoop`.

[`-  scheduleInRunLoop:forMode:`](/documentation/Foundation/NSMachPort/schedule(in:forMode:))

Schedules the receiver into the run loop mode `mode` of `runLoop`.

### Getting and Setting the Delegate

[`-  delegate`](/documentation/Foundation/NSMachPort/delegate())

Returns the receiver’s delegate.

[`-  setDelegate:`](/documentation/Foundation/NSMachPort/setDelegate(_:))

Sets the receiver’s delegate to a given object.

### Constants

[`Options`](/documentation/Foundation/NSMachPort/Options)

Used to remove access rights to a mach port when the `NSMachPort` object is invalidated or destroyed.

## Relationships

### Conforms To

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`NSCoding`](/documentation/Foundation/NSCoding)

[`Equatable`](/documentation/Swift/Equatable)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

[`Sendable`](/documentation/Swift/Sendable)

[`NSCopying`](/documentation/Foundation/NSCopying)

[`CVarArg`](/documentation/Swift/CVarArg)

### Inherits From

[`Port`](/documentation/Foundation/Port)

---

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)