P. 1
Cocoa Programming (2002)

Cocoa Programming (2002)


|Views: 3,247|Likes:
Published by Weizhong Yang

More info:

Published by: Weizhong Yang on May 11, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less






Objective-C does not allow multiple-implementation inheritance, but multiple-interface inheritance is supported through
protocols. A class can adopt any number of protocols, and protocols themselves can adopt other protocols. To declare
that one protocol adopts another, just include the adopted protocol names in angle brackets (< and >) after the protocol
declaration using the following syntax:


Any object that conforms to PROTOCOLNAME also conforms to all the protocols in PROTOCOL-LIST.

Protocol Objects

Protocols are similar to classes because they both declare methods. The Objective-C runtime encapsulates class
definitions with class objects. Protocols are encapsulated by protocol objects. Apple's Objective-C runtime encapsulates
protocols with a class called Protocol. The compiler creates class objects automatically from class declarations, and
creates protocol objects automatically from protocol declarations.

References to class objects can be stored in variables and passed as arguments to methods. References to instances of
the Protocol class can be used in the same ways. The @protocol() compiler directive accesses the instances of
the Protocol class that are stored in the runtime as follows:

Protocol *aProtocol = @protocol(UpDown);

The variable, aProtocol, is a pointer to an instance of the Protocol class, and is initialized to reference the
protocol called UpDown.

Protocols in Distributed Messaging

Another important use of protocols is to optimize distributed messaging. Messages can be sent to objects in a different
process on a different computer. The Objective-C runtime routinely sends messages between anonymous objects, and
has little knowledge about either the sender of the message or the receiver. Nothing special is done with the parameters
and return values of messages sent to objects in the same process, but the runtime must package the messages'
arguments and return value for network transport.

If the runtime does not know enough information about the receiver of a distributed message to correctly package the
parameters and return value, the runtime must interrogate the remote object to get that information. The interrogation
consumes some of the network bandwidth and performance.

Protocols can be used to optimize distributed messaging. The runtime can ask the remote object if it conforms to a
particular protocol. Subsequent distributed messages that are defined by the protocol can be efficiently packaged and
sent over the network. The protocol conformance only needs to be checked once, and that one check verifies the
existence and types of all the methods declared in the protocol.

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->