First of all let me point out that you do not need to create a separate instance variable with an underscore prefix when you use @property
to declare a property. You can access this property using self.delegate
and it also automatically creates _delegate
for you. Because _delegate
is already created using @property
you can take out the duplicate declaration.
Secondly, you can move <SampleProtocolDelegate>
to the property declaration, you should also set it to weak to avoid a retain cycle. See: Why use weak pointer for delegation?. So your interface would end up looking like this:
@interface SampleProtocol : NSObject
@property (nonatomic, weak) id <SampleProtocolDelegate> delegate;
-(void)startSampleProcess;
@end
By putting <SampleProtocolDelegate>
between ‘id’ and ‘delegate’,
only objects that conform to the SampleProtocolDelegate can set themselves as the delegate of the object (it means: any object that conforms to this protocol). And the SampleProtocol object can safely assume that it can call the protocol methods on its delegate.
1
solved how delegates works and delegates work flow in objective-c