Rules of Optimization: Rule 1: Don't do it. Rule 2 (for experts only): Don't do it yet.

Protocols and Delegates

Congratulations, you have acquired the skills to become an iOS developer! However,
there are two additional topics that iOS developers need to understand to be successful:
protocols and delegates. It is not uncommon for new developers to get overwhelmed by
these topics, so we thought it best to introduce the foundation topics of the Objective-C
language first and conclude this book with protocols and delegates.


Multiple Inheritance

We discussed object inheritance in Chapter 1. In a nutshell, object inheritance means
that a child can inherit all the characteristics of its parent. See Figure 14–1.


Figure 14–1. Typical Objective-C inheritance
14

Java and C++ have a feature called multiple inheritance. Multiple inheritance enables a
class to inherit behaviors and features from more than one parent. See Figure 14–2.
However, problems can arise with multiple inheritance because it allows for ambiguities
to occur. Because of this, Objective-C does not implement multiple inheritances.
Instead, it implements something called a protocol.


Figure 14–2. Multiple inheritance


Understanding Protocols


Apple defines a protocol simply as a list of methods declarations, unattached to a class
definition. The methods listed for protocols are suppose to be implemented by you. For
example, these methods that report user actions for the mouse could be placed into a
protocol. See the following example:
- (void)mouseDown:(NSEvent *)theEvent;
- (void)mouseDragged:(NSEvent *)theEvent;
- (void)mouseUp:(NSEvent *)theEvent;
Any class that wanted to respond to mouse events could adopt the protocol and
implement its methods.



Protocol Syntax

The interface example for a protocol is
@protocol WebServiceDelegate
- (void)connetionURL;
- (void)connectionComplete;
@end
The implementation file for this protocol example would be
@interface MyClass : SomeSuperClass < WebServiceDelegate >
@end
Any object that wants to implement the WebServiceDelegate protocol would include
< WebServiceDelegate > after object definition.
It is not uncommon for iOS developers to have multiple protocols for their objects. This
adds real power to your objects when needed.
@interface MyClass : UITableViewController <CLLocationManagerDelegate, WebService,
UITextFieldDelegate, UITextViewDelegate, UITableViewDataSource>
{
}
This example illustrates the power of delegates. MyClass is able to handle all methods
from CLLocationManagerDelegate, WebService, UITextFieldDelegate,
UITextViewDelegate, and UITableViewDataSource.
This means that MyClass is able to use all the methods in these protocols. This includes
the following:
􀀁 GPS information
􀀁 Our custom web services
􀀁 Text field handling
􀀁 Table view handing
The methods from these protocols are called delegates methods.


Understanding Delegates

Delegates are helper objects. They enable us to control the behavior of our objects. The
methods listed in the protocol become helpers to our MyClass.
We can now use these methods in our object. For example, including the
<CLLocationManagerDelegate> protocol in our MyClass enables our object to get notified
by the iPhone’s GPS of our new location. The following example shows the method that
we will include and define inside our object’s implementation file:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation

{
……
}
The locationManager delegate method automatically gets called as our GPS location
changes, allowing your code to process the new and old coordinates. Listing 14–1 is an
example of how to implement didUpdateToLocation and didFailWithError delegate
methods from CLLocationManagerDelegate.in our class MyCoreLocationController
Listing 14–1. Core location delegate example
@implementation MyCoreLocationController //our own controller
@synthesize locationManager;
- (id) init {
self = [super init];
if (self != nil) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self; // send location updates updates to myself
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Location: %@", [newLocation description]);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Error: %@", [error description]); //print error description
}
- (void)dealloc {
[self.locationManager release];
[super dealloc];
}
@end


Next Steps

You now have a great Objective-C foundation. You should be able to dive right
becoming a great iOS developer. Two great books that we recommend to students as
they progress to becoming iOS developers are Learn Objective-C on the Mac by Mark
Dalrymple and Scott Knaster, and Beginning iPhone Development by Dave Mark and
Jeff LaMarche, both published by Apress.
You will be well prepared for these books and writing your iOS apps. However, don’t
take time off – keep moving forward. Get started with these books and writing your
apps. The faster your start using what you have learned, the better you will get.
Whatever you do, don’t stop now!



Summary

You made it! In this chapter we covered why multiple inheritance is not used in
Objective-C and how protocols and delegates work.
There is still a lot to learn and know on your iOS journey. Keep it up and help others
along their way.
You should be familiar with the following terms:
1. Multiple Inheritance
2. Protocol
3. Delegate

  Objective C1
  Objective C2
  Objective C3
  Making Decisions About…and Planning Program Flow
  Object Oriented Programming with Objective-C
  Introducing Objective-C and Xcode
  Objective-C Classes, Objects, and Methods
  Programming Basics in Objective-C
  Comparing Data
  Creating User Interfaces with Interface Builder
  Memory, Addresses, and Pointers
  Debugging Programs with Xcode
  Storing Information
  Protocols and Delegates




affiliate_link