I created a protocol and a class to use the protocol.
JSTest.h file =
@protocol JSTestDelegate
@end
@interface JSTest : NSObject {
id<JSTestDelegate> delegate;
}
@property (nonatomic, retain) id<JSTestDelegate> delegate;
- (id)initWithDelegate:(id<JSTestDelegate>)del;
@end
JSTest.m file =
#import "JSTest.h"
@implementation JSTest
@synthesize delegate;
- (id)initWithDelegate:(id<JSTestDelegate>)del {
self = [super init];
if(self) {
self.delegate = del;
}
return self;
}
- (void)dealloc {
[delegate release];
[super dealloc];
}
My issue is – in the dealloc method, the
[delegate release]
gives me a warning
-release not found in protocol(s)
I am unable to determine the reason. My code should not fail because the delegate will always be a subclass of NSObject. However, I have had bad experiences with warnings that I ignored without understanding the reason for the warning to show up.
There’s a
NSObjectprotocol that you can inherit from (reference here). That protocol contains the basic methods of anyNSObject.By making your own protocol comply with it, you’ll be able to call
-releasewithout getting any compiler warning.Sample code: