Various Questions About This Chapter


#1

Firstly, what would be an example as to when to actually use a delegate? Why would it be worth doing over subclassing? At what point would you succumb to actually subclassing instead?

  1. Do we usually use an init method to assign a delegate like how it was done for the NSURLConnection? Are there other ways?

  2. Could we have created a designated delegate object to use as a helper object containing various methods that all objects requiring a delegate can use?? So a single delegate for various objects with specific methods for each object it delegates for?

  3. In the section about Object Ownership we have an example of dealloc using ‘setDelegate/setDatasource’ methods. Having looked at the Apple Docs I see these methods in NSPort, NSKeyedArchiver, NSSpellServer… so what about other objects? How to they go about this?

please…


#2

Good examples of when to use delegates: NSApplication, UIApplication, NSTableView, UITableView, etc.

See also Protocols, Programming with Objective-C: developer.apple.com/library/ios … 0-CH11-SW1.

Also consider the following.

Imagine that you want to write a function to sort an array of integers, you could write several prototypes for it:

void SortNumbersAscending (long *numbers, unsigned long count);

void SortNumbersDescending (long *numbers, unsigned long count);

void SortNumbers (long *numbers, unsigned long count, bool shouldSortAscending);

void SortNumbers (long *numbers, unsigned long count, bool (*IsLessThan)(long number1, long number2));

The last function is the simplest example of the use of a delegate, the delegate being passed in the last parameter, IsLessThan, a pointer to a function that determines in what sense number1 is less than number2.

Of all the four functions above, the last one is the most flexible one: the the function itself does not need to know anything about how to compare two numbers in the array; the function simply trusts the delegate to do the comparison.


#3

My brother, I really appreciate your response but your answers have not helped me at all? I think your mind is supremely more adept in the understanding of these concepts than mine and I am still climbing the ladder so I need things explained in a way I can make sense of, hence my specific questions which I do think hard about before I asked them as well as writing them as intrinsically as I can.

e.g I wouldn’t even know how to appreciate that " NSApplication, UIApplication, NSTableView, UITableView, etc " are good examples of delegate use!!
We have not learned anything about them yet in the book and even though I have briefly studied the apple docs Im still trying to get a grip on the syntax let alone these higher level concepts!

I started learning in March 2013 having only a few years of electronics experience so Im still trying to understand this brand new world. If you could please guide me through my questions I would understand so much more and learn so much quicker.
I really thank you for your time!


#4

Don’t worry, it won’t be much long before you meet those classes: NSApplication, UIApplication, NSTableView, UITableView, etc.

Also take it easy and go slow; learning to write code takes time, even longer if you dove straight into Objective-C, without any solid background in a high-performance programming language: C or C++.

You are done, if you understand the concept of delegation, that is, one agent consulting another agent for information or asking it to do something.

[quote][code]void SortNumbersAscending (long *numbers, unsigned long count);

void SortNumbersDescending (long *numbers, unsigned long count);

void SortNumbers (long *numbers, unsigned long count, bool shouldSortAscending);

void SortNumbers (long *numbers, unsigned long count, bool (*IsLessThan)(long number1, long number2));[/code][/quote]Look at those sorting functions. Do you understand what’s going on there?

[quote]Why would it be worth doing over subclassing? At what point would you succumb to actually subclassing instead?[/quote]If you understand the difference between the two concepts, you can easily answer your own question.

Delegation and subclassing are entirely different concepts. Delegation is similar to asking a subcontractor to do a job for you, whereas subclassing is similar to creating a new entity like that subcontractor with the intention of modifying its business.

[quote]2) Do we usually use an init method to assign a delegate like how it was done for the NSURLConnection? Are there other ways?
[/quote]No. Yes.

[quote]3) Could we have created a designated delegate object to use as a helper object containing various methods that all objects requiring a delegate can use?? So a single delegate for various objects with specific methods for each object it delegates for?[/quote]Yes, you could create a jumbo delegate but that would not be such a good idea? For example, when would you stop adding more to it? And what name would you give to it? Better to have lots of delegates, each one an expert in its own domain.

Instead better to spend your time reading: Programming with Objective-C:
developer.apple.com/library/ios … 10-CH1-SW1


#5

Thank you, Ive been studying your answers very carefully all morning

Sorting Functions) I have a rough understanding according to the method parameters but without seeing the implementation I couldn’t tell whats going on in the methods themselves but I can see the logic behind the required parameters until I look at the 4th method with the delegate and then I wonder what “long number1, long number2)” are for which throws me off… (also I’m guessing “long *numbers” is “long *numbers[]” right?) The 3rd seems the most logical to me to be honest… and this is the first time I’m seeing delegate use in pure C so it seems fuzzy. I’m used to “thinking” in Objective-C even though I spent about 3 months enjoying learning C…!

  1. Ok, I think I’m clear now that its more efficient to use delegation whenever possible over subclassing. I guess it makes life easier for the delegating object like not making it do things it was not designed to do but Im still trying to get my head around more benefits of delegation. I can see how a delegate could communicate the same message to more than one object at a time which could be a good thing. I guess more experience with writing programs should reveal the real-world reasons for using delegation patterns.

  2. I need to learn more about how we assign delegates as up until now I have only done it as the book showed us with the ‘logger’ Class using the init method of NSURLConnection and assigning the logger as the delegate…

  3. I guess it could be called “The big bad all seeing, all knowing, all doing, all present mammy jammy delegate” … I see your point :wink:

  4. Yes I will study that document this week and learn from it!

I understood ‘protocols’ much sooner than I thought I would have and it helped me to visualise(to a degree) how the mechanics of MVC work.

Your time is appreciated and put to good use by me.
Theres so much to learn but Im really taking to it and because Im a deep conceptual thinker I spend time meditating on the bigger picture and I can see right through the levels of a software experience, from the user interacting with the interface and straight into the transistors and tiny units of current within the EMF…bliss. Its actually refreshing to learn code because unlike most humans code doesn’t lie, cheat, go behind your back or leave you when you need it most and its comforting and honest!
Ive given up my previous life to study Objective-C/IOS full-time and Im planning on doing so until Im working professionally. I have talent and am a naturally creative and technical person so Im confident Im heading into the right field. Im 33.

Thank You ibex10, catch up soon…
ugur