Reputation: 3722
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?
Upvotes: 32
Views: 21607
Reputation: 2762
Other than using CFGetRetainCount
you can use _getRetainCount
as well. The advantages of _getRetainCount
are following:
_getRetainCount
is available as part of Swift's standard library so you don't have to be dependant on Foundation
._getRetainCount
is the only choice as CFGetRetainCount
is not available there.Note that _getRetainCount
provides total of strong, weak and unowned reference counts. If you only want strong reference count you can use _getUnownedRetainCount
and _getWeakRetainCount
to deduct.
As for practical use cases, the only use case I have for this is to write tests for catching retain cycles.
Upvotes: 2
Reputation: 2082
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
Upvotes: 62
Reputation: 2762
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?
Upvotes: 3