NSPort is an abstract class that represents a communication channel. Communication occurs between
NSPort objects, which typically reside in different threads or tasks. The distributed objects system uses
NSPort objects to send
PortMessage objects back and forth. You should implement interapplication communication using distributed objects whenever possible and use
NSPort objects only when necessary.
- iOS 8.0+
- macOS 10.10+
- tvOS 9.0+
- watchOS 2.0+
NSPort object receives a port message, it forwards the message to its delegate in a
handle(_:) message. The delegate should implement only one of these methods to process the incoming message in whatever form desired.
handleMachMessage(_:) provides a message as a raw Mach message beginning with a
handle(_:) provides a message as an
PortMessage 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
Foundation defines three concrete subclasses of
MessagePort allow local (on the same machine) communication only.
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
NSMachPort object is created instead.