I am new to objective-c and pretty much confused with the way delegates are used. I came across this code and i would like to use it to clarify some of the doubts.
#import <Foundation/Foundation.h>
@protocol ProcessDataDelegate <NSObject>
@required
- (void) processSuccessful: (BOOL)success;
@end
@interface ClassWithProtocol : NSObject
{
id <ProcessDataDelegate> delegate;
}
@property (retain) id delegate;
-(void)startSomeProcess;
@end
Here, there are 2 references to the word “delegate” can anyone explain what it means when used in context with “id angled bracket delegate angled-bracket-closed” ??
Again, there is a property with name delegate. Should it not cause any kind of conflict ??
Thanks in advance,
Nope, there is just one property, the one defined by
@property (retain) id delegate; the other one is (sort of) private variable of theNSObjectwhich is not a property in a objective-c sense… then, you have@synthetizekeyword in your .m file, which expands the property.The other notation,
id<protocol>is telling, that delegate is expected to conform to a certain protocol.You can access both from the class, but you can access only property from outside. Also, for a property, some other stuff is generated – in case of retain (when not using ARC), memory management routines.
To complete the (confusion?) lecture, there is a nice way to have class private properties, when you define them in .m file instead of .h in an anonymous category like: