sirab333
sirab333

Reputation: 3722

Swift: how to log out retain count of objects?

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

Answers (4)

Soumya Mahunt
Soumya Mahunt

Reputation: 2762

Other than using CFGetRetainCount you can use _getRetainCount as well. The advantages of _getRetainCount are following:

  1. _getRetainCount is available as part of Swift's standard library so you don't have to be dependant on Foundation.
  2. On non-apple platforms _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

Tritmm
Tritmm

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

Subhajit
Subhajit

Reputation: 13

print(_getRetainCount(objectName))

Upvotes: -3

InkGolem
InkGolem

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

Related Questions