Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
I'm working through Hillegass' book, and it's bringing up delegates. The problem is, it never defines what a delegate is and because of it I can't complete one of the exercises. Can anyone please help?
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Delegates are objects that implement all or part of an 'informal protocol', and attach themselves as the delegate object for something else (see the -delegate and -setDelegate: methods).

Each object that can have a delegate will use it differently, and have a different informal protocol. NSTableView and NSApplication can both have delegates for example, but each one expects its delegate to implement different methods.

If you want to know what delegate methods a particular class supports, you will need to look at the class reference documentation for that class.
 

aLoC

macrumors 6502a
Nov 10, 2006
726
0
It's a way of customizing one of Apple's classes. Basically you make your own object and tell Apple's object about it. Then Apple's object calls yours at certain times, thus *in effect* you have added your code to Apple's object.

In normal OO you would subclass a class to add your code to it. This is just a different way of doing it (a "design pattern") that gives the same effect without the need to subclass.
 

cmaier

Suspended
Jul 25, 2007
25,405
33,474
California
I think he defines what a delegate is, but does so by calling it a "helper class." Then he switches to referring to it as a delegate later on.
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
It's a way of customizing one of Apple's classes. Basically you make your own object and tell Apple's object about it. Then Apple's object calls yours at certain times, thus *in effect* you have added your code to Apple's object.

In normal OO you would subclass a class to add your code to it. This is just a different way of doing it (a "design pattern") that gives the same effect without the need to subclass.

Delegates have one advantage, you can have one delegate object for multiple other objects. In some cases, it is useful.
 

liptonlover

macrumors 6502a
Original poster
Mar 13, 2008
989
0
yes he does have a section about helper objects... that makes sense. Thanks everyone!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.