iOS Developer Library

Developer

Foundation Framework Reference NSMachPort Class Reference

Options
Deployment Target:

On This Page
Language:

NSMachPort

NSMachPort is a subclass of NSPort that can be used as an endpoint for distributed object connections (or raw messaging). NSMachPort is an object wrapper for a Mach port, the fundamental communication port in OS X. NSMachPort allows for local (on the same machine) communication only. A companion class, NSSocketPort, allows for both local and remote distributed object communication, but may be more expensive than NSMachPort for the local case.

To use NSMachPort effectively, you should be familiar with Mach ports, port access rights, and Mach messages. See the Mach OS documentation for more information.

Inheritance


Conforms To


Import Statement


Swift

import Foundation

Objective-C

@import Foundation;

Availability


Available in iOS 2.0 and later.
  • Creates and returns a port object configured with the given Mach port.

    Declaration

    Swift

    class func portWithMachPort(_ machPort: UInt32) -> NSPort

    Objective-C

    + (NSPort *)portWithMachPort:(uint32_t)machPort

    Parameters

    machPort

    The Mach port for the new port. This parameter should originally be of type mach_port_t.

    Return Value

    An NSMachPort object that uses machPort to send or receive messages.

    Discussion

    Creates the port object if necessary. Depending on the access rights associated with machPort, the new port object may be usable only for sending messages.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    class func portWithMachPort(_ machPort: UInt32, options options: Int) -> NSPort

    Objective-C

    + (NSPort *)portWithMachPort:(uint32_t)machPort options:(NSUInteger)options

    Parameters

    machPort

    The Mach port for the new port. This parameter should originally be of type mach_port_t.

    options

    Specifies options for what to do with the underlying port rights when the NSMachPort object is invalidated or destroyed. For a list of constants, see Mach Port Rights.

    Return Value

    An NSMachPort object that uses machPort to send or receive messages.

    Discussion

    Creates the port object if necessary. Depending on the access rights associated with machPort, the new port object may be usable only for sending messages.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    convenience init(machPort machPort: UInt32)

    Objective-C

    - (instancetype)initWithMachPort:(uint32_t)machPort

    Parameters

    machPort

    The Mach port for the new port. This parameter should originally be of type mach_port_t.

    Return Value

    Returns an initialized NSMachPort object that uses machPort to send or receive messages. The returned object might be different than the original receiver

    Discussion

    Depending on the access rights for machPort, the new port may be able to only send messages. If a port with machPort already exists, this method deallocates the receiver, then retains and returns the existing port.

    This method is the designated initializer for the NSMachPort class.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    init(machPort machPort: UInt32, options options: Int)

    Objective-C

    - (instancetype)initWithMachPort:(uint32_t)machPort options:(NSUInteger)options

    Parameters

    machPort

    The Mach port for the new port. This parameter should originally be of type mach_port_t.

    options

    Specifies options for what to do with the underlying port rights when the NSMachPort object is invalidated or destroyed. For a list of constants, see Mach Port Rights.

    Return Value

    Returns an initialized NSMachPort object that uses machPort to send or receive messages. The returned object might be different than the original receiver

    Discussion

    Depending on the access rights for machPort, the new port may be able to only send messages. If a port with machPort already exists, this method deallocates the receiver, then retains and returns the existing port.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • machPort machPort Property

    The Mach port used by the receiver, represented as an integer. (read-only)

    Declaration

    Swift

    var machPort: UInt32 { get }

    Objective-C

    @property(readonly) uint32_t machPort

    Discussion

    The Mach port used by the receiver. Cast this value to a mach_port_t when using it with Mach system calls.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func removeFromRunLoop(_ runLoop: NSRunLoop, forMode mode: String)

    Objective-C

    - (void)removeFromRunLoop:(NSRunLoop *)runLoop forMode:(NSString *)mode

    Parameters

    runLoop

    The run loop from which to remove the receiver.

    mode

    The run loop mode from which to remove the receiver.

    Discussion

    When the receiver is removed, the run loop stops monitoring the Mach port for incoming messages.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

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

    Declaration

    Swift

    func scheduleInRunLoop(_ runLoop: NSRunLoop, forMode mode: String)

    Objective-C

    - (void)scheduleInRunLoop:(NSRunLoop *)runLoop forMode:(NSString *)mode

    Parameters

    runLoop

    The run loop to which to add the receiver.

    mode

    The run loop mode in which to add the receiver.

    Discussion

    When the receiver is scheduled, the run loop monitors the mach port for incoming messages and, when a message arrives, invokes the delegate method handleMachMessage:.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 2.0 and later.

  • Returns the receiver’s delegate.

    Declaration

    Swift

    func delegate() -> NSMachPortDelegate?

    Objective-C

    - (id<NSMachPortDelegate>)delegate

    Return Value

    The receiver’s delegate.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 4.0 and later.

  • Sets the receiver’s delegate to a given object.

    Declaration

    Swift

    func setDelegate(_ anObject: NSMachPortDelegate?)

    Objective-C

    - (void)setDelegate:(id<NSMachPortDelegate>)anObject

    Parameters

    anObject

    The delegate for the receiver.

    Import Statement

    Objective-C

    @import Foundation;

    Swift

    import Foundation

    Availability

    Available in iOS 4.0 and later.

    See Also

    – delegate

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

    Declaration

    Swift

    var NSMachPortDeallocateNone: Int { get } var NSMachPortDeallocateSendRight: Int { get } var NSMachPortDeallocateReceiveRight: Int { get }

    Objective-C

    enum { NSMachPortDeallocateNone = 0, NSMachPortDeallocateSendRight = (1 << 0), NSMachPortDeallocateReceiveRight = (1 << 1) };

    Constants

    • NSMachPortDeallocateNone

      NSMachPortDeallocateNone

      Do not remove any send or receive rights.

      Available in iOS 2.0 and later.

    • NSMachPortDeallocateSendRight

      NSMachPortDeallocateSendRight

      Deallocate a send right when the NSMachPort object is invalidated or destroyed.

      Available in iOS 2.0 and later.

    • NSMachPortDeallocateReceiveRight

      NSMachPortDeallocateReceiveRight

      Remove a receive right when the NSMachPort object is invalidated or destroyed.

      Available in iOS 2.0 and later.