Extension of the Category

Source: Internet
Author: User

Categories of extended continuation categories decentralized implementation code
    • In a large project, the implementation of a class can be very large, and. m files cannot be detached. However, the use of categories allows the implementation of a class to be dispersed and organized in different files. You can also spread the implementation of a class into different frameworks.
    • Programmers can read code more easily and implement multi-person collaboration coding
    • Version management reduces conflicts
    • Maintenance personnel and easy to understand code

In the header file of the code, if there are so many member variables, you will need the appropriate setter, and getter method

#import <Foundation/Foundation.h> @interface things:nsobject{nsuinteger thing1;    Nsuinteger thing2;    Nsuinteger Thing3;    Nsuinteger Thing4; Nsuinteger Thing5;} -(void) resetallvalue;-(void) print;-(void)setThing1:(nsuinteger) thing1;-(nsuinteger) thing1;-(void)setThing2:(nsuinteger) thing2;-(nsuinteger) thing2;-(void)SetThing3:(nsuinteger) thing3;-(nsuinteger) thing3;-(void)SetThing4:(nsuinteger) Thing4;-(nsuinteger) Thing4;-(void)setThing5:(nsuinteger) thing5;-(nsuinteger) thing5;@end

This corresponds to. M file will be cumbersome to no, this is 5 variables, if it is 100, need a lot of people to complete, can not let everyone hold the core files, so it is necessary to disperse them, and the category gives us a mechanism, only need a total implementation of the file. M, Some of these methods are then dispersed into their category files to implement. Below, the header file Things.h file to modify the code as follows:

 @interface things : nsobject {Nsuinteger thing1;    Nsuinteger thing2;    Nsuinteger Thing3;    Nsuinteger Thing4; Nsuinteger Thing5;} - (void) resetallvalue;-(void) Print;the extension of the//class is as follows @interface things (thing1)- (void) SetThing1: (Nsuinteger) thing1;-(Nsuinteger) thing1;@end @interface things (thing2)- (void) SetThing2: (Nsuinteger) thing2;-(Nsuinteger) thing2;@end @interface things (thing3)- (void) SetThing3: (Nsuinteger) thing3;-(Nsuinteger) thing3;@end @interface things (thing4)- (void) SetThing4: (Nsuinteger) thing4;-(Nsuinteger) Thing4;@end @interface things (thing5)- (void) SetThing5: (Nsuinteger) thing5;-(Nsuinteger) thing5;@end

Implementing the content of the file THINGS.M only requires the core approach, and the rest allows it to expand the category to do:

#import "Things.h"  @implementation things - (void) print{NSLog(@"%ld,%ld,%ld,%ld,%ld", Self. Thing1, Self. Thing2, Self. Thing3, Self. Thing4, Self. Thing5);} - (void) resetallvalue{ Self. Thing1= -; Self. Thing2= $;}@end

The rest of the concrete implementation, that needs a lot of people to do the part, to separate them, below a things+thing1.m file

#import "Things.h"@implementation Things (Thing1)- (void)setThing1:(NSUInteger)t{    thing1 = t;}- (NSUInteger)thing1{    return thing1;}@end
Creating forward references and method declarations from categories
    • Cocoa does not have a true private method, but if you know the name of a method that the object supports, you can call it even if the method is not declared in the interface of the class where the object resides. Because you can use categories to tell the compiler that this method has already been declared.
    • This feature of the category is often used in jailbreak projects to complete the invocation of the private API.
Response Selector
    • The so-called response selector can be understood as the type of the method.
    • The response selector is also an attribute of the OBJECTIVE-C runtime.
    • You can use selectors to determine whether an object can execute a method or to test whether an object can respond to a specified message.
    • The response selector can be interpreted as a method or a type of message, so it can be used as a parameter of a method. It can also be stored as an instance variable, very flexible.
    • Use the @ selector () compiler directive to specify the selector, which is the specific method name in parentheses. For example:
@selector(setEngine:)@selector(setTire:atIndex)
    • Type keyword for selector: SEL
    • (BOOL) Respondstoselector: (SEL) aselector;//Use this method to determine whether an object can execute a specified method.
      Example code:
        QYCar *car = [[Car alloc] init];        if([car respondsToSelector:@selector(setEngine:)]){            NSLog(@"hihi");         }
Commission and informal agreements
    • The so-called delegate is actually a technical implementation where an object has another object requesting to perform certain operations.
    • The so-called informal agreement , in fact, is to create a nsobject category.

Below, I'll use a simple example to implement the informal protocol implementation delegate
The first step is to create a class named Qyxiaoming that inherits from the NSObject class, creating a Qyxiaohong class that implements one of these features:
Xiao Ming study tired, want to let Little red in his sleep after a certain period of time after he called up, this is the small red called Xiao Ming This action is a commission. The specific code is as follows:

QYXiaoMing.h's Content

#import <Foundation/Foundation.h>@interface QYXiaoMing : NSObject//小明学习累了,有学习的行为- (void)startSleep:(NSUInteger) time;@end//QYXiaoMing.h

QYXIAOMING.M's Content

#import "QYXiaoMing.h"#import "QYXiaoHong.h"@implementation QYXiaoMing- (void)startSleep:(NSUInteger)time{    NSLog(@"Sleep...");    QYXiaoHong *xiaohong = [[QYXiaoHong alloc] init];    [xiaohong call:time];}@end//QYXiaoMing.m

Header file QYXiaoHong.h

#import <Foundation/Foundation.h>@interface QYXiaoHong : NSObject- (void)call:(NSUInteger)time;@end//QYXiaoHong.h

Concrete implementation of QYXIAOHONG.M

#import "QYXiaoHong.h"@implementation QYXiaoHong- (void)call:(NSUInteger)time{    //需要在指定的时间里把小明叫起来(计时器的用法)    [NSTimer  scheduledTimerWithTimeInterval:time target:self selector:@selector(callXiaoMing) userInfo:nil repeats:NO];}@end//QYXiaoHong.m

Category header File Nsobject+callxiaoming.h

#import <Foundation/Foundation.h>@interface NSObject (CallXiaoMing)- (void)callXiaoMing;@end

Category implementation File NSOBJECT+CALLXIAOMING.M

#import "NSObject+CallXiaoMing.h"@implementation NSObject (CallXiaoMing)- (void)callXiaoMing{    NSLog(@"lalala,don‘t sleep!");}@end

MAIN.M's Code

#import <Foundation/Foundation.h>#import"QYXiaoMing.h"int main(intconstchar * argv[]) {    @autoreleasepool {        QYXiaoMing *xiaoming = [[QYXiaoMing alloc] init];        [xiaoming startSleep:5];//5是5秒        //要求程序运行到这里,不能结束, 是为了让显示的时间循环显示下        [[NSRunLoop currentRunLoop] run];    }    return0;}//main.m

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Extension of the Category

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.