initWithReceivePort:sendPort:
Returns an NSConnection object initialized with given send and receive ports.
Declaration
- (instancetype) initWithReceivePort:(NSPort *) receivePort sendPort:(NSPort *) sendPort;Parameters
- receivePort:
The receive port for the new connection.
- sendPort:
The send port for the new connection.
Return Value
An NSConnection object initialized with receivePort and sendPort. The returned object might be different than the original receiver.
Discussion
The new NSConnection object adds receivePort to the current NSRunLoop object with NSDefaultRunLoopMode as the mode. If the application doesn’t use an NSApplication object to handle events, it needs to run the NSRunLoop object with one of its various run... messages.
This method posts an NSConnectionDidInitializeNotification once the connection is initialized.
The receivePort and sendPort parameters affect initialization as follows:
If an
NSConnectionobject with the same ports already exists, returns it and discards the original receiver.If an
NSConnectionobject exists that uses the same ports, but switched in role, then the newNSConnectionobject communicates with it. Messages sent to a proxy held by either connection are forwarded through the otherNSConnectionobject. This rule applies both within and across address spaces.
This behavior is useful for setting up distributed object connections between threads within an application. See Distributed Objects Programming Topics for more information.
If
receivePortandsendPortarenil, deallocates the receiver and returnsnil.If
receivePortisnil, theNSConnectionobject allocates and uses a new port of the same class assendPort.If
sendPortisnilor if both ports are the same, theNSConnectionobject usesreceivePortfor both sending and receiving and is useful only for vending an object. Use registerName: and rootObject to vend an object.If an
NSConnectionobject exists that usesreceivePortas both of its ports, it’s treated as the parent of the newNSConnectionobject, and its root object and all its configuration settings are applied to the newNSConnectionobject. You should neither register a name for nor set the root object of the newNSConnectionobject. See Configuring a Connection for more information.If
receivePortandsendPortare different and neither is shared with anotherNSConnectionobject, the receiver can be used to vend an object as well as to communicate with otherNSConnectionobjects. However, it has no otherNSConnectionobject to communicate with until one is set up.The
receivePortparameter can’t be shared byNSConnectionobjects in different threads.
This method is the designated initializer for the NSConnection class.