类别的创建
integer.h
@interface integer : NSObject @property int integer; @end
integer.m
@implementation integer @end
类别
integer+display.h
integer+display.m
#import <Cocoa/Cocoa.h> #import "integer.h" NS_ASSUME_NONNULL_BEGIN // 类别 没有成员变量 @interface integer(display) //原有类名(类别名) -(void)show; -(void)add:(integer*)other; -(void)sub:(integer*)other; -(void)mul:(integer*)other; -(void)div:(integer*)other; @end
#import "integer+display.h" @implementation integer(display) -(void)show { // NSLog(@"hello world"); NSLog(@"%i",self.integer); } -(void)add:(integer*)other { self.integer+=other.integer; } -(void)sub:(integer*)other { self.integer-=other.integer; } -(void)mul:(integer*)other { self.integer*=other.integer; } -(void)div:(integer*)other { if(other.integer!=0) { self.integer/=other.integer; } } @end
字符串的反转
#import <Foundation/Foundation.h> #import "integer.h" NS_ASSUME_NONNULL_BEGIN @interface NSString(reverse)// 原有类名(类别名) -(NSString*)reverseString; @end
#import "integer+reverse.h" @implementation NSString(reverse) -(NSString*)reverseString { //将原有字符串反转 创建常量字符串无法修改 需要提前添加缓冲区 进行赋值操作 //1.将字符串内容转换成c语言字符串 char* str=[self UTF8String]; //2.讲c语言字符串反转 int len=strlen(str); char* new=(char*)malloc(len+1); memset(new, 0, len+1); while(*str !='