Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: how to log out retain count of objects?

Tags:

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?

like image 569
sirab333 Avatar asked Mar 10 '15 20:03

sirab333


People also ask

How do I find the retain count in Swift?

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).

What is retain count?

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.

How do I check my reference count?

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 .

What is retain count in IOS?

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.


2 Answers

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

like image 198
Tritmm Avatar answered Oct 10 '22 15:10

Tritmm


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?

like image 24
InkGolem Avatar answered Oct 10 '22 15:10

InkGolem