O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

For the More Curious: Can I Retrieve the Reference Count of an Instance?

Unfortunately, Swift does not give you access to the actual reference count of any instances. (Although recall from Chapter 18 that you can ask if a variable is the only reference to an instance via the isKnownUniquelyReferenced(_:) function.)

Even if you could ask an instance what its reference count is, the answer you get might not be what you expect. Throughout this chapter, we said things like, “At this point, the reference count is 2.” That was a white lie.

Conceptually, it is perfectly reasonable for you to think of reference counts the way we described. Under the hood, the compiler is free to insert additional calls to retain (increment the reference count) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required