<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="">When not using a capture list, Swift closures always refer to variables, not to their values. So here you’re not capturing copies, you’re capturing the actual variable ‘test’, and the mutating function will update it. (If you <i class="">did</i>&nbsp;capture a copy, it would be immutable, and the call to ‘newNum(new:)’ would be rejected.)</div><div class=""><br class=""></div><div class="">On top of that, ‘test’ is a top-lovel variable, so it doesn’t even need to be captured at all. The closures can just refer to it globally.</div><div class=""><br class=""></div><div class="">Hope that clears things up. There are some&nbsp;<a href="https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/Expressions.html#//apple_ref/doc/uid/TP40014097-CH32-ID544" class="">more examples of this</a>&nbsp;in the Swift Programming Language book.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 30, 2016, at 02:38, Седых Александр via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">
<div class=""><p style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" data-mce-style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" class="">Assume we have this code:</p><div style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" data-mce-style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" class="">Immediately two Timer's closures captured two copy of initial values of instance Test, with internal num values is 0.</p><p style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545; min-height: 14px;" data-mce-style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545; min-height: 14px;" class="">&nbsp;<br class=""></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #008400;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #008400;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">//: Playground - noun: a place where people can play</span></p><div style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">import</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class=""> UIKit</span></p><div style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #d12f1b;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #d12f1b;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #000000;" class=""> str = </span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">"Hello, playground"</span></p><div style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class=""> Test {</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class=""> num = </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" class="">0</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">mutating</span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">func</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class=""> newNum(new: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">) {</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" class="">num</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class=""> = new</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; }</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">}</span></p><div style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">var</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class=""> test = </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" class="">Test</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">()</span></p><div style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;" class="">Timer</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #3e1e81;" class="">scheduledTimer</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">(withTimeInterval: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" class="">1.0</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">, repeats: </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">false</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">) { (timer) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">in</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #3e1e81;" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #d12f1b;" class="">"tick"</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">)</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" class="">test</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #31595d;" class="">newNum</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">(new: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" class="">8</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">)</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">}</span></p><div style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #703daa;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #703daa;" class="">Timer</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #3e1e81;" class="">scheduledTimer</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">(withTimeInterval: </span><span style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #272ad8;" class="">2.0</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">, repeats: </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">false</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">) { (timer) </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #ba2da2;" class="">in</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #d12f1b;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #d12f1b;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #000000;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #3e1e81;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #3e1e81;" class="">print</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #000000;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">"tack, test.num = </span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #000000;" class="">\</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" class="">test</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #000000;" class="">.</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #4f8187;" class="">num</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">)"</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #000000;" class="">)</span></p><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">}</span></p><div style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><div style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #3e1e81;" data-mce-style="margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: #3e1e81;" class=""><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures;" class="">CFRunLoopRun</span><span style="font-variant-ligatures: no-common-ligatures;" data-mce-style="font-variant-ligatures: no-common-ligatures; color: #000000;" class="">()</span></p><p style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545; min-height: 14px;" data-mce-style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545; min-height: 14px;" class=""><br class=""><br class=""><br class=""></p><p style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" data-mce-style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" class="">We have next log:</p><p style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" data-mce-style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" class=""><strong class="">tick</strong></p><p style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" data-mce-style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" class=""><strong class="">tack, test.num = 8</strong></p><div style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: rgb(69, 69, 69); min-height: 14px;" class="">&nbsp;<br class="webkit-block-placeholder"></div><p style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" data-mce-style="margin-bottom: 0px; font-size: 12px; line-height: normal; font-family: 'Helvetica Neue'; color: #454545;" class="">Why Timer's two closure return to us value 8 and not 0?</p><br class=""><br class=""><br class="">-- <br class="">Седых Александр</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>