//
// 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;
}
转载于:https://www.cnblogs.com/0515offer/p/4586943.html