From C ++ to objective-c -- delegate

Source: Internet
Author: User

Delegation is often used in iOS development framework cocoa touch. It means that a class object requires the delegate object to perform some of its operations. The delegated mechanism is actually a design mode, through which the coupling between objects can be reduced. 1 is the concept description.

This article mainly introduces the nsobject base class in objective-C. It is an entry for beginners to understand the ID type,

Familiar with objective-C courses, read the IOS design mode directly-delegation Mode


// Environment

// Mac OS X 10.3.7

// Xcode version 4.2.1

// The command line tool project of xcode is used, and the result is output by the command line.

Code (Click here to download)

@ Interface myframework: nsobject {}@ property (nonatomic, assign) nsobject * delegate; // delegate Object Pointer, type: nsobject, that is, all objective-C class parent classes-(ID) Init;-(void) calldelegate; @ end

# Import "myframework. H "@ implementation myframework @ synthesize delegate;-(nsstring *) Description {return (@" I am myframework ") ;}- (ID) Init {self = [Super init]; if (Self) {} return self;}-(void) calldelegate {// The description method nsstring * string = [self. delegate description]; // in the latest version of the compiler, nslog only accepts formatted strings for Security (supports arc), because nslog uses printf to format output at the underlying layer. // Nslog (STR); // warning // nslog (@ "% @", STR); // solution // nslog (STR, nil ); // solution nslog (string, nil );}

@interface Delegate : NSObject@property NSInteger number;-(id)init;@end#import "Delegate.h"@implementation Delegate@synthesize number;-(NSString *)description{    self.number = self.number + 1;    NSString *string = [[NSString alloc] initWithFormat:@"I am Delegate,%ld calls",self.number];    return (string);}-(id)init{    self = [super self ];    if (self) {        self.number =0;    }    return self;}@end

First, two classes are defined. The myframework class uses the delegate class to access the description method of delegate. It can be seen from this that objective-C is purely object-oriented. If you are familiar with C ++'s shoes, you will find that the same functions above are difficult to complete, and C ++ does not have a common parent class, so there is no pointer that can point to any delegate object, which seems to be an unsolved problem. However, C ++ has the class member function pointer concept. The specific implementation can be viewed in this blog post. It is very clever to implement multiple parameters. The ID type can be directly used in a specific application,
Generally applied to the framework, the Protocol is also included, for example:

Delegate composed of classes and protocols in the core location framework (mobile device positioning.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.