Instance variable name wrong?


#1

Hello,

On the bottom of page 79 the method looks like this:

I copied the delegate method out of the documentation. And it looked like that:

Because mapView is already declared as instance variable in WhereamiAppDelegate.h, I get a warning “warning: local declaration of ‘mapView’ hides instance variable”.

I changed the instance variable name, because the delegate method has to be exact the same. I redid the connection between the application delegate and the map view in Interface Builder.

The application runs without the changes thou, but I think there shouldn’t be any warnings while building.

BTW: I really enjoy the book so far.


#2

Yes, the name of a delegate method must match the name it has been declared as in the delegate protocol. However, the names of the arguments do not need to match. Why is that? An instance of MKMapView, when an annotation is added to it, sends the message mapView:didAddAnnotationViews: to its delegate object. That looks just like you would expect:

[[self delegate] mapView:self didAddAnnotationViews:someViews];

The names of the arguments are no matter, it is only the message that is being dispatched. Remember that with delegation, there is no magic, there is just a message being sent.

I’d say the best way to really grasp this is to write your own class that uses a delegate.

Here is a “bonus challenge”: Make a copy of the Whereami project, so you can fiddle without losing any of your work.

Add a new method to the MapPoint class, something trivial, like - (void)becomeHipSpot.
Set it up so MapPoints have a delegate.
When you create an instance of MapPoint (in locationManager:didUpdateToLocation:fromLocation:), set its delegate to be the WhereamiAppDelegate and send the MapPoint the message becomeHipSpot.
Create a delegate protocol for MapPoint that has one method, - (void)mapPointDidBecomeHipSpot:(MapPoint *)mp;
Have this message be sent to a MapPoint’s delegate whenever a MapPoint is sent the message becomeHipSpot. (To make sure it works, put an NSLog in WhereamiAppDelegate’s implementation of mapPointDidBecomeHipSpot:).

Now, at this point, this will actually be a difficult challenge. When you finally solve it, you will most likely not believe you solved it because of how simple the solution is. But once you have solved it and believed you have solved it (which, of course, you can double-check by posting here), you will be at an entirely new level of understanding in the Objective-C world.


#3

I tried the “bonus” and it is working and I’m comfortable with the solution.

One question I have, though, is that I get a warning (respondsToSelector: not found in protocol) on the line of code checking to see if the delegate responds to the selector:

- (void)becomeHipSpot
{
	NSLog(@"becomeHipSpot called");
	if ([[self delegate] respondsToSelector:@selector(mapPointDidBecomeHipSpot:)])
		[[self delegate] mapPointDidBecomeHipSpot:self];
}

Is there a reason I get this warning?

Thanks!

BTW, love the book so far!


#4

You will need to add the following to your protocol declaration:

[code]
@protocol MyProtocol

Basically, that appends all of the methods in the protocol NSObject to MyProtocol.


#5

Thanks for the response, makes a lot of sense now.


#6

hi there,

I’ve been trying and trying on the bonus challenge but cannot get things to work, please can you post a solution?

thanks, David