Protocol Methods


#1

I am coming from the C#/.NET world and have two questions for you more experienced Objective C aficionados!

The code at the top of page 85 confuses me for the following reason: The code pertains to CLLocationManager implementing a method that is checking (using respondsToSelector:) to see if another method is implemented by CLLocationManager. Why would CLLocationManager NOT know what other methods it implements? …I can understand the WhereamiAppDelegate class not knowing what CLLocationManager implements, but why does CLLocationManager need to use the respondsToSelector: to see what other methods it implements?

Also, the last paragraph on page 84 starts off with “Before sending an optional method,”. Should this read “Before sending an optional message”?

Thank you!


#2

In regard to my first question above, I realize now that the respondsToSelector: call is being made on the delegate NOT on the instance of CLLocationManager


#3

Optional methods in a protocol is methods that are optional to implement. You can still conform to the protocol without implementing and using these optional methods. So, if some object were to use these optional methods or the delegate of that object, it would be wise to first check that the object / delegate has that method implemented, since it isn’t a guarantee it is implemented due to being optional.

Of course you could say “well i KNOW it implements it coz i just wrote it”, but thinking in a bigger picture, you might make more calls specified on other conditions, and this also functions as a fail-safe, since calling a method that isn’t implemented causes a crash.

Hope i helped.
Best Regards
/JBJ
Follow me on - www.jbjprogramming.com