Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

need help understanding delegates and delegation in iOS

Posted on 2013-01-19
2
476 Views
Last Modified: 2013-01-20
I have been reading through the Big Nerd Ranch book and in chapter 4, on delegation, I have gone through and read it and done the coding but I still don't understand what delegates are.

Can someone help me understand delegation and its purpose and why I need to know it? What are the consequences of not knowing it? Thanks
0
Comment
Question by:Mark_Co
2 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 38798397
Who is a delegate in the real life?
One man acts in behalf of someone else.
Same in Objective-C.
One object wants another object to perform a specific task - this second object is the delegate, the description of the task is the protocol.

In Objective-C, we:
1. define a protocol (keyword @protocol)
2. add id <my_protocol> delegate  property to a class
3. instantiate an object of this class
4. implement the protocol in another object (the method are declared in the protocol and implemented in the delegate).
5. assign this last object to be the delegate in the first object.
Now from the first object we can call the methods defined in the protocol.

Here is a very short example that just illustrates the pattern:
#import <Foundation/Foundation.h>

@protocol MyProtocol <NSObject>
- (void)print:(NSInteger)number;
@end

@interface MyClass : NSObject

@property (assign) id <MyProtocol> delegate;

- (void)doSomething;

@end

@implementation MyClass

- (void)doSomething
{
    NSInteger x = 10;
    if ( [self delegate] ) {
        [[self delegate] print:x];
    }
}

@end

@interface MyDelegate : NSObject <MyProtocol>

@end

@implementation MyDelegate

- (void)print:(NSInteger)number
{
    NSLog(@"number = %li", number);
}

@end

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        MyClass *first = [[MyClass alloc] init];
        MyDelegate *second = [[MyDelegate alloc] init];
        [first setDelegate:second];
        
        [first doSomething];
        
        [second release];
        [first release];
    }
    return 0;
}

Open in new window


In Xcode, you can create new project from OS X Command Line Tool (Foundation) template and replace the code in main.m file. The output is following:
2013-01-20 15:33:28.113 TestDelegate[2620:303] number = 10
0
 

Author Closing Comment

by:Mark_Co
ID: 38799531
Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
"Reset" on Apple devices 2 83
iOS10 Features 2 49
sms in cloud 7 40
iPhone 5s not receiving phone calls or texts until minutes or hours later 11 50
In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Apple Developer Reference describes NSInvocation this way: An NSInvocation is an Objective-C message rendered static, that is, it is an action tur…
When Apple released Swift last year, the aim was to introduce a new programming language for Cocoa and Cocoa Touch that was fast, easy and effective, like the name connotes. Apple succeeded. Swift is designed to couple with Objective-C program…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question