[swift-evolution] [Proposal] Allow using optional binding to upgrade self from a weak to strong reference

Kurt Werle kurt at circlew.org
Sat Feb 20 01:52:13 CST 2016


Arg!  My mistake - I did not realize you can't test an unowned to be nil
like you can an implicitly unwrapped variable.

On Fri, Feb 19, 2016 at 11:03 PM, Shawn Erickson <shawnce at gmail.com> wrote:

> True, however only if unowned works in terms of the desired object life
> time / "ownership" for self. It isn't always sufficient so weak is needed
> to avoid the retain cycle while safely supporting self becoming nil (e.g.
> allowing the object referenced by self to go away while the block may still
> be outstanding). The proposal outlines at least one example of that need
> and is written assuming you need to use weak.
>
> On Fri, Feb 19, 2016 at 10:15 PM Kurt Werle <kurt at circlew.org> wrote:
>
>> On Fri, Feb 19, 2016 at 9:04 PM, Evan Maloney <emaloney at gilt.com> wrote:
>>
>> ...
>>
>>> With this feature, the code above could be rewritten as:
>>>
>>> networkRequest.fetchData() { [weak self] result in
>>>     guard let self = self else { return }
>>>
>>> If you're going to have boilerplate like that, then the following seems
>> like it accomplishes the same thing [in this specific case] without
>> changing anything:
>>
>> networkRequest.fetchData() { [unowned self] result in
>>>     guard self != nil else { return }
>>>
>>>
>> Kurt
>> --
>> kurt at CircleW.org
>> http://www.CircleW.org/kurt/
>>
>


-- 
kurt at CircleW.org
http://www.CircleW.org/kurt/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160219/cf43f6c2/attachment.html>


More information about the swift-evolution mailing list