Implementation code about the IOS classification (category)

Source: Internet
Author: User

Its essence is the expansion of a class, there are two ways to extend the class:

(1) by inheritance (often used)

(2) by category

A known class name

Its header file Name.h

#import <Foundation/Foundation.h>

@interface Name:nsobject

@property (strong,nonatomic) nsstring* nameStr;

-(Name *) init: (nsstring*) namestring;

-(void) namevalue;

@end

its implementation file NAME.M

#import "Name.h"

@implementation Name

-(Name *) init: (nsstring*) namestring

{

Name * name = [name new];

Name.namestr = namestring;

return name;

}

-(void) namevalue

{

NSLog (@ "----%@--", SELF.NAMESTR);

}

@end

Create a new header file name+change.h is used to declare the method that it wants to expand

Source:

#import "Name.h"

@interface Name (change)

-(void) Addnameid: (NSString *) str;

@end

Create a new implementation file NAME+CHANGE.M a way to implement your own expansion.

#import <Foundation/Foundation.h>

#import "Name+change.h"

#import "Name.h"

@implementation Name (change)

-(void) Addnameid: (NSString *) str

{

NSLog (@ "---%@--%@--", self.namestr,str);

}

@end

Its test file Selection command line

Main.m

#import <Foundation/Foundation.h>

#import "Name+change.h"

int main (int argc, const char * argv[]) {

@autoreleasepool {

Name * name1 = [[Name alloc] init:@ "Wangfei"];

[Name1 Namevalue];

[Name1 addnameid:@ "898989"];

}

return 0;

}

Its test results:

2015-07-11 16:34:53.051 category[1462:60100]----wangfei--

2015-07-11 16:34:53.053 category[1462:60100]---wangfei--898989--

Program ended with exit code:0

Implementation code about the IOS classification (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.