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.