I'm new to objective-c and iOS development and in my class I'm declaring delegate protocol.
I found several examples of doing it and they all look very similar but have some differentiations that I want to make clear for myself and understand.
Example 1:
(links - https://stackoverflow.com/a/12660523/2117550 and https://github.com/alexfish/delegate-example/blob/master/DelegateExample/CustomClass.h)
MyClass.h
#import <BlaClass/BlaClass.h>
@class MyClass; // removed in example 2
@protocol MyClassDelegate <NSObject>
@optional
- (void) myClassDelegateMethod:(BOOL)value;
@end
@interface MyClass : NSObject
@property (nonatomic, weak) id <MyClassDelegate> delegate;
@end
MyClass.m
#import "MyClass.h"
@implementation MyClass
@synthesize delegate; // removed in example 2
- (void) myMethodToDoStuff {
[self.delegate myClassDelegateMethod:YES];
}
@end
Example 2: (links - http://www.tutorialspoint.com/ios/ios_delegates.htm)
Actually is the same except these two differences..
Things that differ them:
- In example 1 we declare
@class
before protocol, is it really necessary? or just best practice. Second example works fine without this declaration. - In example 1 we use
@synthesize delegate
as I understand it makes getters/setters for the property but do we really need it? Second example works without this.
Both examples work fine I just want to remove the confusion rising in me.
Thanks!