First, create a new project to test the change in reference count.
Second, find the following path build phases---->compile Sources---->APPDELEGATE.M
Three, select the APPDELEGATE.M file.
Four, set APPDELEGATE.M's compiler flags to "-fno-objc-arc".
This is because we are going to write our test code in the APPDELEGATE.M file. The project, by default, is the arc-managed memory. So, we'll use the parameter "-fno-objc-arc" to set it to manually manage memory.
Five, enter the following code in the APPDELEGATE.M:
| 1 2 3 5 6 7 8 9 10 11 12 13 14 15 16 |
<span style="font-size: 18px;">- (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;}</span> |
Six, begin to write the code. and run. Output results;
| 1 2 3 4 |
<span style="font-size: 18px;">2015-07-14 21:35:46.252 ARC下引用计数的变化Demo[2070:86317] object-12015-07-14 21:35:46.252 ARC下引用计数的变化Demo[2070:86317] object-22015-07-14 21:35:46.253 ARC下引用计数的变化Demo[2070:86317] object-1</span> |
Resources:
iOS Development advanced-Tang Qi.
How to view the change of reference count in arc-environment