How to view reference count changes in the ARC environment?
1. Create a new project to test the changes in the reference count.
2. Find the following path: Build Phases ----> Compile Sources ----> AppDelegate. m
3. Select the AppDelegate. m file ,.
4. Set Compiler Flags of AppDelegate. m to "-fno-objc-arc "..
This is because we need to write our test code in the AppDelegate. m file. By default, the project manages the memory by ARC. Therefore, we use the parameter "-fno-objc-arc" to set it to manual memory management.
5. Enter the following code in AppDelegate. m:
- (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;}
6. Start coding. And run. Output result:
21:35:46. 252 Demo of reference count change under ARC [2070: 86317] object-12015-07-14 21:35:46. 252 Demo of reference count change under ARC [2070: 86317] object-22015-07-14 21:35:46. 253 Demo of reference count change under ARC [2070: 86317] object-1
References:
Advanced iOS development-Tang Qiao.