Is there a way to quickly log out the retain count of objects to Xcode's Console? If not, what's the next best alternative?
You can always check the reference count of a variable by using the CFGetRetainCount function. Note: Reference counting is done only to manage memory for reference types (e.g., classes and closures).
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.
CFGetRetainCount() can be used to get the referenceCount of an object. You can do this by putting break points or using print(CFGetRetainCount(CFTypeRef!)) function in your code .
Retain Count represents number of owners for a particular object. It is zero till object does not have any owners. Increase in one ownership claim will cause retain count to increase by 1 and decrease will cause it to decrement by 1.
using CFGetRetainCount
function
Example:
// `CFGetRetainCount` is only available in the `Foundation` module import Foundation print(CFGetRetainCount(object))
Read more here : https://developer.apple.com/reference/corefoundation/1521288-cfgetretaincount
hope helpful
Typically you would use instruments to get the retain count. But as answered here the method is retainCount
.
How to get the reference count of an NSObject?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With