一。新創建一個project。用於測試引用計數的變化。app
二,找到例如如下路徑Build Phases---->Compile Sources---->AppDelegate.mide
三,選中AppDelegate.m文件,如圖所看到的。post
四。將AppDelegate.m的Compiler Flags設置爲"-fno-objc-arc".如圖所看到的。ui
這是因爲咱們要在AppDelegate.m文件裏寫咱們的測試的代碼。而project默認是ARC管理內存的。因此,咱們將其用參數「-fno-objc-arc",將其設置爲手動管理內存。spa
五,在AppDelegate.m中輸入例如如下代碼:code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSObject *object=[[NSObject alloc]init]; NSLog(@"object-%ld",[object retainCount]); NSObject *another=[object retain]; NSLog(@"object-%ld",[object retainCount]); [another release]; NSLog(@"object-%ld",[object retainCount]); [object release]; return YES; }
六,開始進行代碼的編寫。並且執行。輸出結果如圖所看到的:blog
2015-07-14 21:35:46.252 ARC下引用計數的變化Demo[2070:86317] object-1 2015-07-14 21:35:46.252 ARC下引用計數的變化Demo[2070:86317] object-2 2015-07-14 21:35:46.253 ARC下引用計數的變化Demo[2070:86317] object-1
參考資料:內存
《iOS開發進階》-唐巧著。開發