[Study Notes-Objective-C] Objective-C-Basic tutorial 2nd chapter 1 category, objectivec tutorial
12.1 create category
Category is a way to add a new method to an existing class.
For example, obtain the length of a string and store it in the NSDictionary dictionary.
NSNumber *number; number = [NSNumber numberWithUnsignedInt: [string length]]; // ... do something with number
@interface NSString (NumberConvenience)- (NSNumber *) lengthAsNumber; @end // NumberConvenience
Note::
-(NSNumber *) lengthAsNumber {unsigned int length = [self length]; return ([NSNumber numberWithUnsignedInt: length]); // send lengthAsNumber to the string, returns the NSNumber object of the string length} // lengthAsNumber
Test file:
NSMutableDictionary * dict; dict = [NSMutableDictionary dictionary]; [dict setObject: [@ "hello" lengthAsNumber] forKey: @ "hello"]; [dict setObject: [@ "iLikeFish" lengthAsNumber] forKey: @ "iLikeFish"]; [dict setObject: [@ "Once upon a time" lengthAsNumber] forKey: @ "Once upon a time"]; NSLog (@ "% @", dict); // output: "Key = key content"
Advantage: If you enter a string, send the lengthAsNumber to the string,NowNSNumber object that can return the length of the string
Note::
This category belongs to NSString, so any NSString object can respond to the lengthAsNumber message.
12.11 category defects: 12.12 category advantages: 12.2 class extension)
Class Extension: a special class without a name.
Features:
Example: create Things class Extension
@interface Things : NSObject@property (assign) NSInteger thing1;@property (readonly, assign) NSInteger thign2;
// Obtain the Things class and extend it by adding private attributes and Methods @ interface Things () {NSInteger thing4; // Private} @ property (readwrite, assign) NSInteger thing2; // changed the read and write permissions. Only private methods can be accessed in this class. Only getter method @ property (assign) NSInteger thign3 is available in public interfaces; // Private
Note:
One feature of object-oriented programming is information hiding, which only needs to be presented by users, but not by others. We can put this category in the. m file, or in a private header file.
12.3 implement code with scattered categories
You can use classes to distribute large (with many methods) classes to multiple. m files.
The NSWindow class has hundreds of methods, but uses classification:
@ Interface NSWindow (NSKeyboardUI) // keyboard @ interface NSWindow (NSToolbarSupport) // toolbar @ interface NSWindow (NSDrag) // drag and drop function @ interface NSWindow (NSCarbonExtensions)
Note:
Cocoa designers place data functions in the Foundation framework by category, while plotting is in the Appkit framework.
12.4 create a Forward reference using a category (Forward References)
If you call a method without declaring the method, or are using a method that has not been released in another class, you need a category.
Specific Practices:
Declare in the public interface of the Car classrotateTires
Method, declared in the categorymove
TireFromPosition:toPosition:
To achieverotateTires
RotateTires can be used.
@intreface Car(Private)-(void)moveTireFromPosition:(int) pos1 toPosition: (int) pos2;@end // Private
Note:: Put in the respective @ implementation file
12.5 informal agreements and delegation types
Informal protocols are a type of NSObject that lists the methods that an object can respond. Informal protocols are used to implement Delegation
A delegate is an object that performs some work by another type of requests. Delegation is a technology that allows you to easily customize laid-off behaviors.
Example:
When a user clicks a row, before the tableView object is ready to be executed, it will ask whether the delegate object can select this row.
12.51 ITUnesFinder Project (omitted)
// New NSNetServiceBrowser object: NSNetServiceBrowser * browser; browser = [[NSNetServiceBrowser alloc] init]; // new ITunesFinder object ITunesFinder * finder; finder = [[ITunesFinder alloc] init]; // tells the net browser to use the ITunesFinder object as the proxy [browser setDelegate: finder]; // tells the browser to search for iTunes and share [browser searchForServicesOfType: @ "_ daap. _ tcp "inDomain: @" local. "];
12.52 delegation and category
Delegation is another type of application. An NSObject category can be sent to the delegate object.
@interface NSObject (NSNetServiceBrowserDelegateMethods)- (void) netServiceBrowserWillSearch: (NSNetServiceBrowser *) browser;- (void) netServiceBrowserDidStopSearch: (NSNetServiceBrowser *) browser;- (void) netServiceBrowser: (NSNetServiceBrowser *) browser didRemoveService: (NSNetService *) service moreComing: (BOOL) moreComing;@end
By declaring it as a NSObject category, NSNetServiceBrowser can send one message to any object (as a proxy object)-informal Protocol
Note:: For informal agreements, you only declare what you want to declare.
12.53 response Selector
Selector: only a method name.
Specify selector:@selector()
;
Car classsetEngine:
Method selector:@selector(setEngine:)
Car classsetTire:atIndex:
Method selector:@selector(setTire:atIndex:)
Whether the query object can respond to a specific message
Car *car = [[Car alloc] init];if([Car respondsToSelector: @selector(setEngine:)]){ NSLog(@"Yes");}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.