Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

need help understanding delegates and delegation in iOS

Posted on 2013-01-19
2
Medium Priority
?
481 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
[X]
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
2 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 2000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
The Basics of Storyboard Storyboards were first introduced to developers in the release of iOS 5. They save the developer the trouble of designing each screen’s interface in a different file. The storyboard allows you to see the conceptual mockup o…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

604 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