<!--
{
  "availability" : [
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/PortMessage",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSPortMessage"
  },
  "title" : "PortMessage"
}
-->

# PortMessage

A low-level, operating system-independent type for inter-application (and inter-thread) messages.

```
class PortMessage
```

## Overview

Port messages are used primarily by the distributed objects system. You should implement inter-application communication using distributed objects whenever possible and use [`PortMessage`](/documentation/Foundation/PortMessage) only when necessary.

An [`PortMessage`](/documentation/Foundation/PortMessage) object has three major parts: the send and receive ports, which are [`Port`](/documentation/Foundation/Port) objects that link the sender of the message to the receiver, and the components, which form the body of the message. The components are held as an [`NSArray`](/documentation/Foundation/NSArray) object containing [`NSData`](/documentation/Foundation/NSData) and [`Port`](/documentation/Foundation/Port) objects. The [`send(before:)`](/documentation/Foundation/PortMessage/send(before:)) message sends the components out through the send port; any replies to the message arrive on the receive port. See the [`Port`](/documentation/Foundation/Port) class specification for information on handling incoming messages.

An [`PortMessage`](/documentation/Foundation/PortMessage) instance can be initialized with a pair of [`Port`](/documentation/Foundation/Port) objects and an array of components. A port message’s body can contain only [`Port`](/documentation/Foundation/Port) objects or [`NSData`](/documentation/Foundation/NSData) objects. In the distributed objects system the byte/character arrays are usually encoded [`NSInvocation`](/documentation/Foundation/NSInvocation) objects that are being forwarded from a proxy to the corresponding real object.

An [`PortMessage`](/documentation/Foundation/PortMessage) object also maintains a message identifier, which can be used to indicate the class of a message, such as an Objective-C method invocation, a connection request, an error, and so on. Use the [`msgid`](/documentation/Foundation/PortMessage/msgid) and [`msgid`](/documentation/Foundation/PortMessage/msgid) methods to access the identifier.

## Topics

### Creating Instances

[`-  initWithSendPort:receivePort:components:`](/documentation/Foundation/PortMessage/init(send:receive:components:))

Initializes a newly allocated `NSPortMessage` object to send given data on a given port and to receiver replies on another given port.

### Sending the Message

[`-  sendBeforeDate:`](/documentation/Foundation/PortMessage/send(before:))

Attempts to send the message before the specified date.

### Getting the Components

[`components`](/documentation/Foundation/PortMessage/components)

Returns the data components of the receiver.

### Getting the Ports

[`receivePort`](/documentation/Foundation/PortMessage/receivePort)

For an outgoing message, returns the port on which replies to the receiver will arrive. For an incoming message, returns the port the receiver did arrive on.

[`sendPort`](/documentation/Foundation/PortMessage/sendPort)

For an outgoing message, returns the port the receiver will send itself through. For an incoming message, returns the port replies to the receiver should be sent through.

### Accessing the Message ID

[`msgid`](/documentation/Foundation/PortMessage/msgid)

Returns the identifier for the receiver.

## Relationships

### Conforms To

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

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

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

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

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

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

### 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)