zoukankan      html  css  js  c++  java
  • OC2_引用计数

    //
    //  Dog.h
    //  OC2_引用计数
    //
    //  Created by zhangxueming on 15/6/18.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    @interface Dog : NSObject
    {
        NSString *_name;
        NSInteger _age;
    }
    
    @property (copy, nonatomic) NSString *name;
    @property (nonatomic)NSInteger age;
    
    @end
    
    
    
    
    //
    //  Dog.m
    //  OC2_引用计数
    //
    //  Created by zhangxueming on 15/6/18.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import "Dog.h"
    
    @implementation Dog
    
    @end
    //
    //  main.m
    //  OC2_引用计数
    //
    //  Created by zhangxueming on 15/6/18.
    //  Copyright (c) 2015年 zhangxueming. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    #import "Dog.h"
    
    //管理内存方式:
    //自动引用计数 automic refence counting
    //手动引用计数 manual refence counting
    
    //gar --> automic refence counting  yes --> no
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            Dog *xiaoBai = [[Dog alloc] init];
            
            NSLog(@"retainCount = %li", xiaoBai.retainCount);
            
            //对对象进项保留, 增加对象的拥有权,使引用计数加1;
            //retain 的前提 是该对象存在
            
            Dog *xiaoHei = [xiaoBai retain];
            //Dog *xiaoHei = xiaoBai; 只是xiaohei的指针指向xiaobai ,并没有retainCount加1.
            NSLog(@"retainCount = %li", xiaoHei.retainCount);
        
            Dog *xiaoFei = [xiaoHei retain];
            NSLog(@"retainCount = %li", xiaoFei.retainCount);
        
            //release 释放对象拥有权, 引用计数器的值-1;
            [xiaoBai release];
            xiaoBai = nil;
            NSLog(@"retainCount = %li", xiaoFei.retainCount);
            
            [xiaoHei release];
            xiaoHei = nil;
            NSLog(@"retainCount = %li", xiaoFei.retainCount);
            
            [xiaoFei release];//retainCount = 0 dealloc
            xiaoFei = nil;
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
    //        NSLog(@"今天天气不错, 就是下了点雨");
        //对象被释放之后, 不能再给对象发送消息
        // NSLog(@"retainCount = %li", xiaoFei.retainCount);
        }
        return 0;
    }
  • 相关阅读:
    笔记本出现故障,蓝屏状态中 沧海
    Form Head Data 沧海
    西门子索毕业生30万违约金败诉 被指培训名不副实 沧海
    Questions about purchasing 沧海
    甲骨文Bosma先生谈托管型CRM 沧海
    有关T型账户 沧海
    到岸成本 沧海
    紧张的学习 沧海
    SOA仍旧看着很美 沧海
    心情变得很糟糕 沧海
  • 原文地址:https://www.cnblogs.com/0515offer/p/4586943.html
Copyright © 2011-2022 走看看