[swift-users] Checking whether an object is uniquely referenced, using a weak reference to it

Jordan Rose jordan_rose at apple.com
Tue Jun 28 11:32:42 CDT 2016

> On Jun 27, 2016, at 18:52, Tim Vermeulen <tvermeulen at me.com> wrote:
> Thanks for your reply. It didn’t clear up everything, though. The official documentation says "Weak references do not affect the result of this function.”, which suggests that weak (and unowned) references intentionally aren’t counted. The docs only mention the implementation of copy-on-write behaviour as a use case (which also happens to be what I’m using it for).

I would expect that weak references are important to count for COW, since you can observe changes through them. Dave?

> Couldn’t there just a be a function that returns the reference count of a given object as an Int? It would make everything a lot easier (i.e. it wouldn’t need inout because it can just create a reference to that object, find the reference count, then subtract 1).

As we’ve said for a long time in Objective-C, asking for the reference count of an object is meaningless. isUniquelyReferenced only works because it’s conservative: because it only checks for “exactly 1”, it’s safe from threading issues and autorelease pools. We do not plan to add a -retainCount equivalent to Swift.


More information about the swift-users mailing list