An abstract class that represents a communication channel.
SDKs
- iOS 2.0+
- macOS 10.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
Framework
- Foundation
Declaration
class Port : NSObject
Overview
Communication occurs between Port
objects, which typically reside in different threads or tasks. The distributed objects system uses Port
objects to send Port
objects back and forth. Implement interapplication communication using distributed objects whenever possible and use Port
objects only when necessary.
To receive incoming messages, Port
objects must be added to an Run
object as input sources. NSConnection
objects automatically add their receive port when initialized.
When an Port
object receives a port message, it forwards the message to its delegate in a handle
or handle(_:)
message. The delegate should implement only one of these methods to process the incoming message in whatever form desired. handle
provides a message as a raw Mach message beginning with a msg
structure. handle(_:)
provides a message as an Port
object, 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()
method.
Foundation defines three concrete subclasses of NSPort
. NSMach
and Message
allow local (on the same machine) communication only. Socket
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 alloc
or port
, an NSMach
object is created instead.
Important
Port
conforms to the NSCoding
protocol, but only supports coding by an NSPort
. Port
and its subclasses do not support archiving.