Objective-C 日记⑤ 内存管理、协议、Category 视频笔记
//内存管理第六课 #import <Foundation/Foundation.h> #import "Person.h" #import "Dog.h" int main(int argc,const char *argv) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; Person *xiaoLi=[[Person alloc] init]; //dog1 1 Dog *dog1=[[[Dog alloc] init] autorelease]; //把Dog1这条狗放在自动释放池里 上面的pool中 //dog1 2 xiaoLi.dog=dog1; //[dog1 release]; 有了autorelease 就不可以在release //因为dog 已经放到pool中 在release 则dog1会清除 [xiaoLi release] [pool release];//对拥有的非0(还存在)变量都release一次 return (0); } //dog.h #import <Foundation/Foundation.h> @interface Dog:NSObject { int _ID; } @property int ID; @end //dog.m #import<dog.h> @synthesize ID=_ID; -(void) dealloc //对象减到0(对象销毁时)自动调用 { NSLog(@"dog dealloc "); } //person.h #import<Foundation/Foundation.h> #import "Dog.h" @interface Person:Object { Dog *_dog; } @property (retain) Dog *dog; @end //person.m #import "Person.h" @implementation Person @Synthesize dog=_dog; -(void) dealloc // 对象减到0时自动调用这条 { self.dog=nil;//对狗的计数器减1 NSLog(@"person dealloc"); [super dealloc]; } @end
协议
//第七课:协议 int main(int argc,char *argv[]) { NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init]; } //只有一个头文件 以Protocol开头 方法没有方法体 继匙自蠳SObject @protocol MyProtocol <NSObject> -(void) init; -(int) updat:(int) time; @end //@optional 可以不实现 缺省 //@required 必须凳迪帜 //eg: //@optional 可实现可不实现 // -(void) init; //要实现的协议的名 用尖括号 多个用逗号隔开 @interface:Foo:NSObject<MyProtocol,MouseListerner,MousKey> 实现协议 id<MyProtocol> test=[[MyTest alloc] init]; if([test respondsToSelector:@select(showInfo)]){ } //案例 //protocol @protocol MyProtocol<NSObject> @optional -(void) print:(int) vlaue;//可选 @required -(int) printValue:(int)value1 andValue:(int)value2; @end //MyTest.h @interface MyTest:NSObject<MyProtocol> -(void) showInfo; @end //MyTest.m -(void) ShowInfo { NSLog(@"Show info is calling"); } //来源于MyProtocol协议 -(int) printValue:(int)value1 andValue:(int) value2 { NSLog(@"print value1 and %d value2 %d",value1,value2); return 0; } -(void) print:(int)value { NSLog(@"print vlaue %d",value); } @end #import <Foundation/Foundation.h> # "MyTest.h" # "MyProtocol.h" int main(int argc,const char *argv[]) { @autorelease{ MyTest *myTest=[[MyTest alloc] init]; [myTest showInfo]; //把print转化成SEL类型的方法 OC所有函数都可以转换成SEL SEL sel=@selector(print:);// 有参数冒号注意 if([myTest respondsToSelector:sel]){ //判断myTest是否响应sel方法(print:) [myTest print:20]; } [myTest pirntValue:10 andValue:290]; [myTest release]; //用协议 id<MyProtocol> myProtocol =[[MyTest alloc] init]; if([myProtocol respondsToSelector:@selector(print:)]){ [myProtocol print:111]; } [myProtocol printValue:193 andValue:103]; [myProtocol release]; } return 0; }
第八章:代理模式
//Dod.h 文件 #import<Foundation/Foundation.h> @protocol DogBark;// 协议前向声明 @class Dog;//@class 表示前向声明一个类 @interface Dog:NSObject { NSTimer *timer;// 定时器 int backCount;//叫的次数 int _ID; id <DogBark> delegate;//任何类型 存储主人 //告诉系统这个delegate是DogBark类型 } @property int ID; @property (assign) id <DogBark> delegate;//指针赋值 @end //方法二用协议来做 //定义一个人和狗通讯的方式protocol @protocol DogBark<NSObject> -(void) bark:(Dog *)thisDog count:(int)count; @end //Dod.m文件 #import "Dog.h" @implementation Dog @synthesize ID=_ID; @synthesize delegate; //构造函数ID 初始化 -(id) init { self=[super init]; if(self){ timer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector: @selector(updateTimer:) userInfo:nil repeats:YES]; //创建一个定时器 每隔1.0秒就调用[self updateTimer:nil] } return self; } -(void) updateTimer:(id) arg { backCount++; NSLog(@"dog bark %d",backCount); //通知狗的主人 [_dog setDelegate:self]; //把self传递到delegate中 [delegate bark:self count:barkCount]; //调用delegate里面的bark:count:方法 //向主人汇报 } @end //Person.h #import<Foundation/Foundation.h> #import "Dog.h" @interface Person:NSObject <DogBark> //实现协议 { DOg *_dog; } @property (retain) DOg *dog; @end //Person.m #import "Person.h" @implementation Person @synthesize dog=_dog; -(void) bark:(Dog *) thisDog count:(int)count { //当狗叫的时候来调用xiaoli人的这个方法 NSLog(@"person this dog %d bark %d",[thisDog ID],count); } -(void) dealloc//当对象清零时 调用 { self.dog=nil; [super dealloc]; } // 重写setDog 告知狗的主人是谁 -(void) setDog:(Dog *)dog { if(_dog!=dog){ [_dog release]; _dog=[dog retain]; //通知_dog的主人是self } } -(Dog *) dog { return _dog; } @end #import<Foundation/Foundation.h> #import "Person.h" #import "Dog.h" int main(int argc,const char *argv[]) { @autoreleasepool{ Person *xiaoLi=[[Person alloc] init]; Dog *dog[[Dog alloc] int]; [dog setID:10]; [xiaoLi setDog:dog]; [dog release]; while(1){ [[NSRunLoop currentRunLoop] run]; } [xiaoLi release]; } return 0; }
第九章:Category
//Category 实际上是对类的扩展 //1实现继承之外的扩展方法机制(给一个类扩展动态的或者静态的一些方法进去)
//不能完全替代继承(原因就是下面的缺点不能扩展字段和变量) 但可以完成继承不能完成的。写起来比继承稍微麻烦但比继承好用 //2可以做函数私有化 //eg: //interface Foo(Private)//Private 可以不写 //-(void) test2; //@end //@implementation Foo //-(void) test{ // [self test2] //} //-(void) test2 //{ // NSLog(@"test2") //} //@end //缺点:类别Category只能扩展函数,消息,不能扩展字段,变量等 //命名规范 //一般Category命名为: // 要扩展类名+扩展变量.[hm] //比如: // NSString+ReverseString.h // NSString+ReverseString.m // // UIImageView+WebCache.h // UIImageView+WebCache.m #import <Foundation/Foundation.h> @implementation NSString (ReverseString) -(id) reverseString { NSUInteger len=[self length]; //self 表示字符串本身 NSMutableString *retStr=[NSMutableString stringWithCapacity:len]; while(len>0){ unichar c=[self characterAtIndex:--len]; //从后去一个字符unichar NSLog(@"c is %C",c);//C国际字符 NSString *s=[NSString stringWithFormat:@"%C",c]; [retStr appendString:s] } return retStr; } @end #import<Foundation/Foundation.h> #import "NSString+ReverseString.h" int main(int argc,const char *argv[]) { @autoreleasepool{ NSString *string="Hello Word"; NSString *retString=[string reverseString]; NSLog(@"%@",retString); } return 0; }
作者:PEPE
出处:http://pepe.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述