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

# Port

An abstract class that represents a communication channel.

```
class Port
```

## Overview

Communication occurs between [`Port`](/documentation/Foundation/Port) objects, which typically reside in different threads or tasks. The distributed objects system uses [`Port`](/documentation/Foundation/Port) objects to send [`PortMessage`](/documentation/Foundation/PortMessage) objects back and forth. Implement interapplication communication using distributed objects whenever possible and use [`Port`](/documentation/Foundation/Port) objects only when necessary.

To receive incoming messages, add [`Port`](/documentation/Foundation/Port) objects to an instance of [`RunLoop`](/documentation/Foundation/RunLoop) as input sources. [`NSConnection`](/documentation/Foundation/NSConnection) objects automatically add their receive port when initialized.

When the [`Port`](/documentation/Foundation/Port) object receives a port message, it forwards the message to its delegate in a [`handleMachMessage(_:)`](/documentation/Foundation/NSMachPortDelegate/handleMachMessage(_:)) or [`handle(_:)`](/documentation/Foundation/PortDelegate/handle(_:)) message. The delegate should implement only one of these methods to process the incoming message in whatever form desired. [`handleMachMessage(_:)`](/documentation/Foundation/NSMachPortDelegate/handleMachMessage(_:)) provides a message as a raw Mach message beginning with a `msg_header_t` structure. [`handle(_:)`](/documentation/Foundation/PortDelegate/handle(_:)) provides a message as an instance of [`PortMessage`](/documentation/Foundation/PortMessage), which is an object-oriented wrapper for a Mach message. If a delegate has not been set, the `NSPort` object handles the message itself.

When you are finished using a port object, you must explicitly invalidate the port object prior to sending it a `release` message. Similarly, if your application uses garbage collection, you must invalidate the port object before removing any strong references to it. If you do not invalidate the port, the resulting port object may linger and create a memory leak. To invalidate the port object, invoke its [`invalidate()`](/documentation/Foundation/Port/invalidate()) method.

Foundation defines three concrete subclasses of `NSPort`. [`NSMachPort`](/documentation/Foundation/NSMachPort) and [`MessagePort`](/documentation/Foundation/MessagePort) allow local (on the same machine) communication only. [`SocketPort`](/documentation/Foundation/SocketPort) allows for both local and remote communication, but may be more expensive than the others for the local case. When creating an `NSPort` object, using [allocWithZone:](/documentation/Foundation/nsport-allocwithzone) or [`port`](/documentation/Foundation/NSPort/port), an [`NSMachPort`](/documentation/Foundation/NSMachPort) object is created instead.

For backward compatibility on Mach, `-[NSPort allocWithZone:]` returns an instance of the [`NSMachPort`](/documentation/Foundation/NSMachPort) class when sent to this class. Otherwise, it returns an instance of a concrete subclass that can be used for messaging between threads or processes on the local machine, or, in the case of [`SocketPort`](/documentation/Foundation/SocketPort), between processes on separate machines.

> Important:
> ``doc://com.apple.foundation/documentation/Foundation/Port`` 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 instances

[allocWithZone:](/documentation/Foundation/nsport-allocwithzone)

Returns an instance of the `NSMachPort` class.

[`+  port`](/documentation/Foundation/NSPort/port)

Creates and returns a new `NSPort` object capable of both sending and receiving messages.

### Validation

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

Marks the receiver as invalid and posts an [`didBecomeInvalidNotification`](/documentation/Foundation/Port/didBecomeInvalidNotification) to the default notification center.

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

A Boolean value that indicates whether the receiver is valid.

### Working with the delegate

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

Sets the receiver’s delegate to a given object.

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

Returns the receiver’s delegate.

### Creating connections

[`-  addConnection:toRunLoop:forMode:`](/documentation/Foundation/NSPort/addConnection:toRunLoop:forMode:)

Adds the receiver to the list of ports monitored by a given run loop for the given input mode.

[`-  removeConnection:fromRunLoop:forMode:`](/documentation/Foundation/NSPort/removeConnection:fromRunLoop:forMode:)

Removes the receiver from the list of ports monitored by `runLoop` in the given input mode, `mode`.

### Setting information

[`-  sendBeforeDate:components:from:reserved:`](/documentation/Foundation/Port/send(before:components:from:reserved:))

This method is provided for subclasses that have custom types of `NSPort`.

[`-  sendBeforeDate:msgid:components:from:reserved:`](/documentation/Foundation/Port/send(before:msgid:components:from:reserved:))

This method is provided for subclasses that have custom types of `NSPort`.

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

The number of bytes of space reserved by the receiver for sending data.

### Port monitoring

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

This method should be implemented by a subclass to stop monitoring of a port when removed from a give run loop in a given input mode.

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

This method should be implemented by a subclass to set up monitoring of a port when added to a given run loop in a given input mode.

### Working with notifications

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

Posted from the [`invalidate()`](/documentation/Foundation/Port/invalidate()) method, which is invoked when the `NSPort` is deallocated or when it notices that its communication channel has been damaged. The notification object is the `NSPort` object that has become invalid. This notification does not contain a `userInfo` dictionary.

### Working with notification messages

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

A message the system sends when a port becomes invalid.

### Data Types

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

Type for the platform-specific native socket handle.

## Relationships

### Conforms To

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

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

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

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

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

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

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

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

### Inherited By

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

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

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

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)