Delegation is the process of assigning a job/operation to some other class and getting results once done. A simple example is shown below.

In order to make use of delegation, we need to do three thing as listed below.

  1. Create a protocol.
  2. Create a delegate property in the class that performs the assigned task.
  3. Once the task is complete, delegate property to be used to return the result.
  4. Implement the protocol.

Creating a protocol

Protocol provides the list of functions to be used in delegating process. A simple protocol is shown below that declares a method for returning the downloaded data.

@protocol EezyFetcherDelegate <NSObject>

- (void)downloadedData:(id)data;

@end

Creating a delegate property

When we ask another class to perform an task, we need to retain which class' object called for performing the task. This is done with the help of delegate property as shown below.

@property(nonatomic, weak)id <EezyFetcherDelegate> delegate;

Returning result

Once the task is performed, we need to return the result. This is done using the delegate property as shown below.

- (void) fetchDataWithURL:(NSURL *)url{
    NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfURL:url];
    if ([self.delegate respondsToSelector:@selector(downloadedData:)]) {
        [self.delegate downloadedData:dict];
    }
}

Implement the protocol

You can implement the protocol by conforming to the protocol,setting the delegate property and defining the method. An example is shown below.

// Conforming to protocol
@interface EezySample : NSObject
 <EezyFetcherDelegate>
@end

@implementation EezySample
//Implementing delegate method

-(void)start{
    EezyFetcher *eezyFetcher = [[EezyFetcher alloc]init];
	// Setting the delegate
    eezyFetcher.delegate = self;
    [eezyFetcher fetchDataWithURL:[NSURL URLWithString:
		@"http://ios.eezytutorials.com/sample-files/sample-dictionary-plist.plist"]];
}

-(void)downloadedData:(id)data{
    NSLog(@"%@",data);
}
@end

Complete Example

A complete example for delegating the download of file and getting results back is shown below.

#import <Foundation/Foundation.h>

@protocol EezyFetcherDelegate <NSObject>

- (void)downloadedData:(id)data;

@end

@interface EezyFetcher : NSObject

@property(nonatomic, weak)id <EezyFetcherDelegate> delegate;

- (void) fetchDataWithURL:(NSURL *)url;

@end

// Implementation
@implementation EezyFetcher

- (void) fetchDataWithURL:(NSURL *)url{
    NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfURL:url];
    if ([self.delegate respondsToSelector:@selector(downloadedData:)]) {
        [self.delegate downloadedData:dict];
    }
}

@end

@interface EezySample : NSObject<EezyFetcherDelegate>

- (void)start;

@end

// Implementation
@implementation EezySample

-(void)start{
    EezyFetcher *eezyFetcher = [[EezyFetcher alloc]init];
    eezyFetcher.delegate = self;
    [eezyFetcher fetchDataWithURL:[NSURL URLWithString:
		@"http://ios.eezytutorials.com/sample-files/sample-dictionary-plist.plist"]];
}

-(void)downloadedData:(id)data{
    NSLog(@"%@",data);
}
@end

int main(int argc, const char * argv[])
{
    //  Autoreleasepool used in main for automatic memory management.
    @autoreleasepool {
		EezySample *eezySample= [[EezySample alloc] init];
        [eezySample start];
    }
    return 0;
}