I was looking for Apple delegation information and protocol documentation to answer this question, but after more than one day I decided to refuse and let you guys do it. I have three classes: HTTPManager, LoginManager and FetchManager. You can probably guess what these classes do, but be explicit ...
- HTTPManager - Exchange NSURLConnection and provide a simple interface for LoginManager and FetchManager to perform authenticated HTTP requests.
- LoginManager / FetchManager is basically the same class, but they respond to HTTPManager messages differently.
The HTTPManager expects the delegate to implement the HTTPManagerDelegate protocol, and both LogManager and FetchManager will do this. The Login and FetchManager classes also provide a protocol for my application delegate so that the data can fully return to the user interface.
In my delegation delegation, init:I initialize both the login and the selection manager and receive the following warnings for both:
warning: class 'MyAppDelegate' does not implement the 'HTTPManagerDelegate' protocol
warning: incompatible Objective-C types assigning 'struct HTTPManager *', expected 'struct LoginManager *'
None of the two initialized classes are derived from HTTPManager, but they implement the HTTPManagerDelegate protocol. A line of code that throws the above warning:
_loginMgr = [[LoginManager alloc] initWithDelegate:self];
So what does the LoginManager method initWithDelegate:return HTTPManager*? There is no inheritance, and my return types are correct, so for me this is some kind of dark form of voodoo that I cannot do better.
. , , , :
@protocol HTTPManagerDelegate
...
@end
@interface HTTPManager : NSObject
{
id <HTTPManagerDelegate> _delegate;
...
}
- (HTTPManager *) initWithDelegate:(id <HTTPManagerDelegate>)delegate;
...
@end
@protocol LoginManagerDelegate
...
@end
@interface LoginManager : NSObject <HTTPManagerDelegate>
{
id <LoginManagerDelegate> _delegate;
...
}
- (LoginManager *) initWithDelegate:(id <LoginManagerDelegate>)delegate;
...
@end
@interface MyAppDelegate : NSObject <NSApplicationDelegate, LoginManagerDelegate, FetchManagerDelegate>
{
LoginManager *_loginMgr;
...
}
...
@end
...
- (MyAppDelegate *) init
{
self = [super init];
if (self)
{
_loginMgr = [[LoginManager alloc] initWithDelegate:self];
...
}
return self;
}
...
.