What is retainCount in Objective-C?

拟墨画扇 提交于 2019-11-27 23:41:28

问题


I have a UITableView as my first screen with a UINavigation controller.

In my first screen I NSLog(@"Home Screen retain Count=%d",[self retainCount]); and it logs 6 in when its viewDidLoad is called.

Is this correct?


回答1:


The retainCount is the number of ownership claims there are outstanding on the object.

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. All of these increment the retainCount.

You relinquish ownership with using “release” or “autorelease”. These decrement the retainCount.

However you should never pay any attention to the value of retainCount, it is at best confusing, at worst misleading. Simply follow the memory management rules - take ownership when you need to keep a reference to an object and relinquish ownership when you are finished, and you wont have a problem.

If you are looking at retainCount, you are going about things the wrong way, and you will simply confuse yourself further.




回答2:


It sounds fine. Why would it be wrong?

In general, trying to determine things from the retain count is a bad idea. There are no rules about the amount of times you can retain an object. The only rule is that each retain must be balanced with a release.



来源:https://stackoverflow.com/questions/1206111/what-is-retaincount-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!