Objective c what is a protocol




















Communication works both ways; objects send messages as well as receive them. For example, an object might delegate responsibility for a certain operation to another object, or it may on occasion simply need to ask another object for information. In some cases, an object might be willing to notify other objects of its actions so that they can take whatever collateral measures might be required. If you develop the class of the sender and the class of the receiver as part of the same project or if someone else has supplied you with the receiver and its interface file , this communication is easily coordinated.

The sender simply imports the interface file of the receiver. The imported file declares the method selectors the sender uses in the messages it sends. A protocol serves this purpose. It informs the compiler about methods the class uses and also informs other implementors of the methods they need to define to have their objects work with yours. Suppose, for example, that you develop an object that asks for the assistance of another object by sending it helpOut: and other messages.

You provide an assistant instance variable to record the outlet for these messages and define a companion method to set the instance variable. Then, whenever a message is to be sent to the assistant , a check is made to be sure that the receiver implements a method that can respond:.

A protocol can be used to declare the methods of an anonymous object , an object of unknown class. An anonymous object may represent a service or handle a limited set of functions, especially when only one object of its kind is needed. Objects are not anonymous to their developers, of course, but they are anonymous when the developer supplies them to someone else.

For example, consider the following situations:. Someone who supplies a framework or a suite of objects for others to use can include objects that are not identified by a class name or an interface file.

Lacking the name and class interface, users have no way of creating instances of the class. Instead, the supplier must provide a ready-made instance. Typically, a method in another class returns a usable object:.

The object returned by the method is an object without a class identity, at least not one the supplier is willing to reveal. For it to be of any use at all, the supplier must be willing to identify at least some of the messages that it can respond to.

The messages are identified by associating the object with a list of methods declared in a protocol. You can send Objective-C messages to remote objects —objects in other applications. Each application has its own structure, classes, and internal logic. As an outsider, all you need to know is what messages you can send the protocol and where to send them the receiver.

An application that publishes one of its objects as a potential receiver of remote messages must also publish a protocol declaring the methods the object will use to respond to those messages.

All it needs is the protocol. Protocols make anonymous objects possible. Without a protocol, there would be no way to declare an interface to an object without identifying its class. If more than one class implements a set of methods, those classes are often grouped under an abstract class that declares the methods they have in common. Each subclass can reimplement the methods in its own way, but the inheritance hierarchy and the common declaration in the abstract class capture the essential similarity between the subclasses.

Classes that are unrelated in most respects might nevertheless need to implement some similar methods. This limited similarity may not justify a hierarchical relationship. For example, you might want to add support for creating XML representations of objects in your application and for initializing objects from an XML representation:. These methods could be grouped into a protocol and the similarity between implementing classes accounted for by noting that they all conform to the same protocol.

Objects can be typed by this similarity the protocols they conform to , rather than by their class. For example, an NSMatrix instance must communicate with the objects that represent its cells. The matrix could require each of these objects to be a kind of NSCell a type based on class and rely on the fact that all objects that inherit from the NSCell class have the methods needed to respond to NSMatrix messages. Alternatively, the NSMatrix object could require objects representing cells to have methods that can respond to a particular set of messages a type based on protocol.

The Objective-C language provides a way to formally declare a list of methods including declared properties as a protocol. Formal protocols are supported by the language and the runtime system.

For example, the compiler can check for types based on protocols, and objects can introspect at runtime to report whether or not they conform to a protocol.

You are commenting using your Twitter account. You are commenting using your Facebook account. Notify me of new comments via email. Notify me of new posts via email. Blog at WordPress. Create the protocol. Interfaces in objective-c are called protocols. Implement the protocol. Then to use the protocol as a fraction or a complex number we do the following: SpikeProtocolTests. Share this: Twitter LinkedIn Facebook. Like this: Like Loading Hi Zak.

There is no Printing. Only the. Thanks Haykel. Protocols can have properties. But you need to implement them yourself. Good question. Thanks for asking. Leave a Reply Cancel reply Enter your comment here If you need a class to adopt multiple protocols, you can specify them as a comma-separated list. We have a delegate object that holds the reference of the calling object that implements the protocol.

In the above example we have seen how the delgate methods are called and executed. Its starts with startAction, once the process is completed, the delegate method processCompleted is called to intimate the operation is completed. In any iOS or Mac app, we will never have a program implemented without a delegate.

So its important we understand the usage of delegates.



0コメント

  • 1000 / 1000