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

Dave Abrahams dabrahams at apple.com
Sun Sep 18 15:20:05 CDT 2016

on Tue Jun 28 2016, Jordan Rose <jordan_rose-AT-apple.com> wrote:

>> 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?

This sort of depends on what you expect the semantics of your weak
reference to be.  It's not possible to take a weak reference to an
Array; you can only take a weak reference to the NSArray it uses as a
backing store.  That's an Objective-C-only idea.

Given an arbitrary NSArray on the Objective-C side, it might turn out to
be an NSMutableArray, so if you want to avoid observing changes to it,
in principle you need to copy it.  So I think it's possible to argue
that the weak reference count is not an issue here.


More information about the swift-users mailing list