Remote Messaging

Objective-C’s method call model lends itself well to distributed systems, where sending messages is the fundamental unit of interaction. The key difference in distributed systems is that objects may exist in different address spaces, and so cannot call each other directly.

Objects that want to receive messages from other processes must specify those messages in a formal protocol. Objective-C provides special keywords to use when writing such a formal protocol that qualify the kind of sharing you will need for that protocol’s message parameters.

You use the keywords to modify (or qualify) the type of a method’s parameter or return value. For example, the keywords out and in modify the parameters in the following declaration:

-(void)getData:(out 
            Data*)data 
            forIndex:(in 
            int)index;

The signature of this method says that index is only passed in and changes to it don’t need to be returned, but the method will modify the value of data.

You can’t use these keywords in class or category declarations. However, if your class or category adopts a protocol that uses a remote messaging keyword, you can repeat the keyword in the method signature of the implementation.

The remote messaging keywords can be grouped into three categories: those for pointer parameters, those for return values, and those for object qualifiers.

Pointer Parameter Qualifiers

This section discusses qualifiers that generally apply to pointer arguments. They tell the compiler how to handle copying values between address spaces so that the pointers can be dereferenced in both spaces and see the same value. The pointer parameter qualifiers are as follows:

in

You will reading directly from the parameter, or will dereference the parameter to read a value but not to write one. This qualifier can be used for non-pointers.

out

You will dereference the parameter to write a value but not to read one. This qualifier applies only to pointers.

inout

You will dereference the parameter both to read and write a value. This qualifier applies only to pointers.

If you do not specify a qualifier, the compiler will assume a parameter is inout.

Certain types have special behavior with pointer qualifiers. For example, when you declare a C-style string with type char *, the runtime system treats this as a value type (with value equal to the character string pointed to) and not a pointer, and ships the entire string across between method calls. If you want to pass a string by reference, you need to add another asterisk to its declaration in the parameter list for the method receiving the string.

The runtime treats Objective-C objects the same way. If you want assignments to the parameter to have an effect back in the calling code, you need to add an asterisk in the parameter declaration. Note that passing objects by reference in this sense is not the same as using the byref qualifier (described later in Section 1.6.3).

Return Value Qualifiers

Qualifying a method’s return type with oneway means the method executes asynchronously—that is, its caller can continue execution without waiting for a response. Use oneway only in conjunction with methods with void return type.

Object Qualifiers

Object qualifiers modify types that describe instances of classes, either dynamically typed as id or statically with a class name, and specify whether the runtime needs to provide for interprocess method calls. The types modified can be either parameters or return types.

bycopy

This parameter or returned object should be copied between address spaces (rather than setting up a proxy for inter-process method calls). The receiving code must know about the class of the object it gets.

byref

The opposite of bycopy. This parameter or returned object should be exchanged by reference, and a proxy should be set up for handling method calls.

The default behavior is specified in the code for the class itself. In most cases the default is byref.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.27.131