zoukankan      html  css  js  c++  java
  • setter getter 方法

    MRC下setter、getter方法写法、重写dealloc方法
    
    @interface People : NSObject 
        @property (nonatomic,strong) NSString *brand; 
        //@property (nonatomic,copy) NSString *brand; 
    @end 
    
    setter:
    -(void)setBrand:(NSString *)brand{
        //如果实例变量指向的地址和参数指向的地址不同
        if (_brand != brand)
        {
            //将实例变量的引用计数减一
            [_brand release];
            //将参数变量的引用计数加一,并赋值给实例变量
            _brand = [brand retain];
            //copy 使用下列方法
            _brand = [brand copy];
        }
    }
    
    getter:
    -(NSString *)brand{
        //将实例变量的引用计数加1后,添加自动减1
        //作用,保证调用getter方法取值时可以取到值的同时在完全不需要使用后释放
        return [[_brand retain] autorelease];
    }
    
    重写dealloc:
    //MRC下 手动释放内存 可重写dealloc但不要调用dealloc 会崩溃
    -(void)dealloc{
        [_string release];
        //必须最后调用super dealloc
        [super dealloc];
    }
    
    - (void) setCaption: (NSString*)input 
    {
        [caption autorelease];
        caption = [input retain]; 
    }
    
    - (void) setPhotographer: (NSString*)input
    {
        [photographer autorelease];
        photographer = [input retain]; 
    }
    
    - (void) dealloc 
    {
        [totalAmount release];
        [super dealloc]; 
    }                

     

  • 相关阅读:
    病毒
    最短母串
    单词
    Censoring
    玄武密码
    Keywords Search
    聚会
    异象石
    暗的连锁
    pat 1048. Find Coins (25)
  • 原文地址:https://www.cnblogs.com/xujinzhong/p/11155081.html
Copyright © 2011-2022 走看看