////Dog.h//oc2_ Reference Count////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import<Foundation/Foundation.h>@interfacedog:nsobject{NSString*_name; Nsinteger _age;} @property (copy, nonatomic) NSString*Name: @property (nonatomic) Nsinteger age;@end////DOG.M//oc2_ Reference Count////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import "Dog.h"@implementationDog@end
////main.m//oc2_ Reference Count////Created by zhangxueming on 15/6/18.//Copyright (c) 2015 zhangxueming. All rights reserved.//#import<Foundation/Foundation.h>#import "Dog.h"//Manage Memory methods://Auto Reference count automic refence counting//Manual reference count manual refence counting//gar--automic refence Counting Yes --NointMainintargcConst Char*argv[]) {@autoreleasepool {Dog*xiaobai =[[Dog alloc] init]; NSLog (@"Retaincount =%li", Xiaobai.retaincount); //keep the object input, increase the ownership of the object, and add 1 to the reference count; //The premise of retain is that the object existsDog*xiaohei =[Xiaobai retain]; //Dog *xiaohei = Xiaobai; Just Xiaohei's pointer is pointing to Xiaobai, and there is no Retaincount plus 1.NSLog (@"Retaincount =%li", Xiaohei.retaincount); Dog*xiaofei =[Xiaohei retain]; NSLog (@"Retaincount =%li", Xiaofei.retaincount); //release releases object ownership, referencing the value of counter -1;[Xiaobai release]; Xiaobai=Nil; NSLog (@"Retaincount =%li", Xiaofei.retaincount); [Xiaohei release]; Xiaohei=Nil; NSLog (@"Retaincount =%li", Xiaofei.retaincount); [Xiaofei release];//retaincount = 0 DeallocXiaofei =Nil;//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining");//NSLog (@ "The weather is fine today, it's Raining"); //after the object is freed, no more messages can be sent to the object//NSLog (@ "Retaincount =%li", xiaofei.retaincount); } return 0;}
Oc2_ reference count