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

Joe Groff jgroff at apple.com
Tue Jun 28 12:33:03 CDT 2016


> On Jun 28, 2016, at 9:32 AM, Jordan Rose via swift-users <swift-users at swift.org> 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?

Passing a weak reference inout goes through a strong optional shadow copy. It wouldn't be safe to directly address the weak reference.

-Joe

>> 
>> 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.
> 
> Jordan
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users



More information about the swift-users mailing list