Objective c 目标c代表
目前我正在从事地理编码工作。地理编码服务始终与学员协作 这么说吧,我有一个 AskingClass和AnsweringClass(地理编码) AskingClass调用AnsweringClass中的函数以返回当前位置的地址 AnsweringClass应该处理和封装地理编码内容。我的问题是,对于所有这些委托,我无法返回到原始函数,询问类调用了该函数。所以我不能轻易地把地址还给你: AskingClass.Address=[AnsweringClass GiveAddress] 我管理它,使用委托进行操作,因此结果会在askingClass中的委托函数(某个地方)中返回。但我对此并不满意。它看起来太大太复杂了 致以最良好的问候Objective c 目标c代表,objective-c,delegates,return-type,Objective C,Delegates,Return Type,目前我正在从事地理编码工作。地理编码服务始终与学员协作 这么说吧,我有一个 AskingClass和AnsweringClass(地理编码) AskingClass调用AnsweringClass中的函数以返回当前位置的地址 AnsweringClass应该处理和封装地理编码内容。我的问题是,对于所有这些委托,我无法返回到原始函数,询问类调用了该函数。所以我不能轻易地把地址还给你: AskingClass.Address=[AnsweringClass GiveAddress] 我管理它,使用委
Klaus Dieter不清楚为什么要使用委托模式。为什么不直接使用直线上升类呢 类似这样的内容(假设您使用PCH文件作为头文件,或者根据需要导入它们): 回答类.h
@interface AnsweringClass:NSObject
- (MyAnswer *)answerThisDude;
@end
@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
AskingClass.h
@interface AnsweringClass:NSObject
- (MyAnswer *)answerThisDude;
@end
@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
然后你可以这样做:
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
无需授权。如果不是为仅GC的目标编写,请不要忘记在
setUpMyStuff
中释放新的应答对象。我省略了-dealloc。让我来解决这个问题。