views:

123

answers:

1

Hello,

i working with geocoding at the moment. The geocoding service allways works with delegates.

So let's say, I've got a

AskingClass and AnsweringClass(geocoding)

The AskingClass calls a function in the AnsweringClass to return the adress of the current location.

AnsweringClass should handle and capsulate the geocoding stuff. My Problem is, with all these delegates, I do not manage to come back to the orginal function, which the asking class has called. So I cannot give easily the adress back:

AskingClass.Adress= [AnsweringClass giveAdress];

I managed it, doing it with delegates, so the result comes back in a delegate function (somewhere) in the askingClass. But I'm not happy with that. It's seems oversized and complex.

with best regards

Klaus-Dieter

+1  A: 

It is unclear why you are using a delegate pattern at all. Why not just use straight up classes?

Something like this (assuming that you are using a PCH file for your header files or otherwise importing 'em as needed):

AnsweringClass.h

@interface AnsweringClass:NSObject
- (MyAnswer *)answerThisDude;
@end

AskingClass.h

@class AnsweringClass;  // just in case you including AskingClass.h before AnsweringClass.h
@interface AskingClass : NSObject
// {
// declare the ivar if you need support for 32 bit "classic" ABI
//    AnsweringClass *theThingThatAnswers;
// }
@property(retain) AnsweringClass *theThingThatAnswers;
@end

Then you can do this:

AskingClass.m

@implementation AskingClass
@synthesize theThingThatAnswers;
- (void) setUpMyStuff  // probably invoked by your designated initializer or app launch handler
{
    self.theThingThatAnswers = [AnsweringClass new];
    MyAnswer *theFirstAnswer = [self.theThingThatAnswers answerThisDude];
}

// don't forget a -dealloc if you aren't running GC'd
@end

No delegation necessary.

bbum
Don't forget to release the new answering object in `setUpMyStuff`, if not writing for a GC-only target.
Peter Hosey
I omitted the -dealloc. Let me fix that.
bbum