<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi, Tim. The purpose of the isUniquelyReferenced checks is to see whether any changes to the properties of the object can be observed from elsewhere in this program. Therefore, I would <i class="">expect</i> it to return ‘false’ when there are weak (or unowned) references present, i.e. I think the bug is that we don’t consider weak or unowned references to count for this check.</div><div class=""><br class=""></div><div class="">If my understanding of the intended behavior is correct, then it doesn’t make sense to check for a uniquely-referenced object through a weak (or unowned) reference, because if that were the only reference, then the object would have already been deallocated.</div><div class=""><br class=""></div><div class="">The particular technical reason for the check failing is related to the inout, as you say: in order to pass the reference to the function inout, it has to be loaded into a strong reference first. The reason the inout is there at all is because otherwise the check would fail: there would be a reference from outside the function in addition to the one for the argument.</div><div class=""><br class=""></div><div class="">Hope that clears things up, and if you agree with my interpretation about weak and unowned references, please file a bug at <a href="http://bugs.swift.org" class="">bugs.swift.org</a>!</div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Jun 27, 2016, at 17:14, Tim Vermeulen via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">class</span><span style="font-variant-ligatures: no-common-ligatures" class=""> EmptyClass {}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> strongReference = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">EmptyClass</span><span style="font-variant-ligatures: no-common-ligatures" class="">()</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">weak</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> weakReference = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">strongReference</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">isUniquelyReferencedNonObjC</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(&</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">strongReference</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// true</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures" class="">isUniquelyReferencedNonObjC</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(&</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">weakReference</span><span style="font-variant-ligatures: no-common-ligatures;" class="">)) </span><span style="font-variant-ligatures: no-common-ligatures; color: #008400" class="">// false</span></div></div><div class=""><br class=""></div><div class="">I expected both print statements to print true.</div><div class=""><br class=""></div><div class="">I realise that this is probably a known limitation of this function. After all, if it worked with weak references too, there’s no point for the parameter to be inout:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> wrappedIsUniquelyReferencedNonObjC<T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">AnyObject</span><span style="font-variant-ligatures: no-common-ligatures" class="">>(</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">_</span><span style="font-variant-ligatures: no-common-ligatures" class=""> object: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -> </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">weak</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> weakObject = object</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);" class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">isUniquelyReferencedNonObjC</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(&weakObject)</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><br class=""></div><div class="">So the fact that this function takes an inout parameter hints at the fact that it doesn’t work with weak references. If my reasoning is correct, it would be nice if the docs stated that it only works with strong references to objects. :)</div><div class=""><br class=""></div><div class="">My question is, is it possible at all to use a weak reference to check whether an object is uniquely referenced or not? Since this approach doesn’t seem to work, I guess it is impossible?</div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>