suppose you had Bumblebee, Jet, and Bird classes. All three, although unrelated, implement the flying behavior, and so you could say they are each a Flyer.

Similar presentations

Presentation on theme: "suppose you had Bumblebee, Jet, and Bird classes. All three, although unrelated, implement the flying behavior, and so you could say they are each a Flyer."— Presentation transcript:

1

2
suppose you had Bumblebee, Jet, and Bird classes. All three, although unrelated, implement the flying behavior, and so you could say they are each a Flyer. Also notice that you could shoot all three with a gun, and so you could also say they are Shootable. In both situations you are using a concept to group unrelatedobjects based upon behaviors. Protocols model these types of situations. A class that wishes to adopt a protocol must implement the methods declared by the protocol. By requiring the adopting class to implement methods, a protocol forms a contract. That contract ensures the runtime that a class that adopts a protocol implements the methods declared by the protocol.

3
A protocol’s syntax is similar to a class’ interface. But unlike when writing a class, you only create a header file and do not create an implementation. A protocol begins with compiler directive and ends with compiler directive. Between the two compiler directives go any method declarations you wish an adopting class to implement. Hopper - (void) hop; - (BOOL) setHopDistance: (long long)

4
Classes adopt protocols. For instance, a Frog class might implement the Hopper protocol. For Frog to adopt Hopper, it must first import the Hopper.h header file. It then includes Hopper in Frog: Amphibian A frog is an amphibian, and so it inherits from its Amphibian parent class. Following the parent class is the protocol the frog adopts. The protocol’s name is surrounded by angle brackets.

5
1.Create a View-based application named SimpleFlyer. 2.Create a new C header file called Flyer.h. 3.Modify Flyer.h to be a Flyer protocol that declares a “fly” method. 4. Create three new Objective-C classes named Bumblebee, Bird, and Jet. Change all three so that they adopt the Flyer protocol

8
5. Modify SimpleFlyerViewController.h so that it knows about all three classes and the protocol by using precompiler directives. Also declare a callFly method and create three properties for each of the three classes. Also create a changeLabel method and be certain you precede the method with the IBAction keyword rather SimpleFlyerViewController.h SimpleFlyerViewController : UIViewController { Bumblebee * myBee; Bird * myBird; Jet * myJet; IBOutlet UILabel * myLabel; (nonatomic,retain) Bumblebee * (nonatomic,retain) Bird * (nonatomic,retain) Jet * (nonatomic,retain) UILabel * myLabel; - (IBAction) changeLabel: (id) sender; - (NSString*) callFly: (id ) 6. Create an IBOutlet for a UILabel and name the property myLabel.

10
8.Double-click SimpleFlyerViewController.xib to open it in Interface Builder. 9.Add a button to the view’s canvas. Double-click the button and change its title to Fly. 10.Add a label to the view’s canvas and resize the label 11.Connect the File’s Owner myLabel outlet to the label Remember, the“outlet” term is simply shorthand for referring to the IBOutlet

13
Properties and Protocols Any classes that then wish to adopt the protocol either must implement custom accessor methods or must synthesize the property. A property’s syntax in a protocol is similar to an interface, only you do not declare an instance variable. Countable (nonatomic, retain) NSNumber * By declaring a property in a protocol, you are informing the compiler that implementing classes must either synthesize the property or create custom accessor methods. But note, any class implementing the protocol must synthesize the property and declare the property as an instance variable in the class’ header.

17
If you wish to call an optional protocol method in another method such as hitInFace, you must check for the method’s existence in the adopting class. -(void) hitInFace: (id ) aFighter { if([aFighter { [aFighter cry]; } The easiest way to ensure a class responds to an optional protocol method is through the respondsToSelector: method.

20
Protocols and id - (void) doAttach: (id) aFloater; - (void) detach: ( ) aFloater; - (void) sinkAFloater: (id ) aFloater; Using id Only One way you can use a protocol as a method parameter is by using the id keyword. The id type is a generic pointer that can point to any object. -(void) doAttach: (id) aFloater - { if([aFloater { [aFloater }

21
Using id with a Protocol Restriction You can restrict a method’s parameter so that it can still take an id, but only if that id’s object adopts the specified protocol. The following detach method illustrates. -(void) detach: ( ) aFloater { [aFloater detach]; } In detach the id parameter is implicit. The method takes any object provided the object implements the Floater protocol. You can also make the id explicit, as the following sinkAFloater method illustrates. -(void) sinkAFloater: (id ) aFloater { [aFloater sink]; }

28
Categories When you want to add methods to a class, you typically extend it. However this solution isn't always perfect, especially if you want to rewrite the functionality of a class that you don't have the source code to. Categories allow you to add functionality to already existing classes without extending them. Ruby also has similar functionality to this.

31
1.There is the Fraction (Math) Fraction (Math). 2.There can only be one category with the same name. Additional cateogies may be added on with different but unqiue names. 3.Categories can't add instance variables. 4.Categories are useful for creating private methods. Since Objective-C has no notion of private/protected/public methods like java does, one has to create categories that hide such functionality. The way this is done is by moving the private methods from your class's header (.h) file to the implementation file (.m)