• ObjectiveC 日记⑤ 内存管理、协议、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; }
  • 相关阅读:
    linux——进程管理
    linux——软件管理
    linux——压缩打包
    linux——输入输出
    linux——ACL控制
    linux——特殊权限
    linux——基本权限
    linux——用户管理
    单源最短路spfa(队列优化)
    getline读取
  • 原文地址:https://www.cnblogs.com/PEPE/p/2655577.html
Copyright © 2020-2023  润新知