February 5, 2016 Andrey

Difference between formal and informal protocols

An informal protocol is a Category on NSObject. Implementation of the methods is optional.

@interface NSObject(NSApplicationNotifications)
- (void)applicationWillFinishLaunching:(NSNotification *)notification;
...
@interface NSObject(NSApplicationDelegate)
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender;
...

Formal protocols are an Extension to the Objective-C language. Methods can be required or optional.

@protocol YourElementsViewDataSource
- (NSUInteger)numberOfElements;
- (CGFloat)sizeOfElementAtIndex:(NSUInteger)segmentIndex;
@optional
- (NSString *)titleForElementAtIndex:(NSUInteger)segmentIndex;
- (BOOL)shouldExplodeElementAtIndex:(NSUInteger)segmentIndex;
@required
- (UIColor *)colorForElementIndex:(NSUInteger)segmentIndex;
@end

This example defines a protocol with three required methods and two optional methods.