<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="">I think this has more to do with the UIKit API than the Swift language, but since we’re discussing it, here’s my 2¢</div><br class=""><div><blockquote type="cite" class=""><div class="">On 04 Dec 2015, at 19:49, Chris Byatt &lt;<a href="mailto:byatt.chris@gmail.com" class="">byatt.chris@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">We should be able to do:</div><div class=""><br class=""></div><div class=""><pre class="default prettyprint prettyprinted" style="margin-top:0px;padding:5px;border:0px;font-size:13px;overflow:auto;width:auto;max-height:600px;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;color:rgb(57,51,24);word-wrap:normal"><code style="margin:0px;padding:0px;border:0px;font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,sans-serif;white-space:inherit" class=""><span class="typ" style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">UIBarButtonItem</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">(</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">title</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="str" style="margin:0px;padding:0px;border:0px;color:rgb(128,0,0)">"Press"</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> style</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">.</span><span class="typ" style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Done</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> target</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="kwd" style="margin:0px;padding:0px;border:0px;color:rgb(0,0,139)">self</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> action</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;"> </span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">{</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
    </span><span class="com" style="margin:0px;padding:0px;border:0px;color:rgb(128,128,128)">// Do Something</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px;">
</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px;">})</span></code></pre></div></div></div></blockquote></div><div class="">If action is just a closure, why is target needed? This example doesn’t match what was proposed in the subject “Selectors to be used as Closures”.</div><div class="">I see a couple solutions to this:</div><div class=""><br class=""></div><div class="">1. Make it UIBarButtonIttem(title:style:action:), and pass any closure. That’s the most flexible, but as David mentioned it makes it too easy to create reference cycles.</div><div class="">2. Keep the target/action pattern, but make action’s type match the expected selector signature. To avoid cycles, the button item would keep a weak reference to self, and only call action if target != nil</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="">class</span> MyController: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UIViewController</span> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">override</span> <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> viewDidLoad() {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">super</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81" class="">viewDidLoad</span>()</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; &nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">navigationItem</span>.rightBarButtonItem = <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UIBarButtonItem</span>(title: <span style="font-variant-ligatures: no-common-ligatures; color: #d12f1b" class="">"Press"</span>, style: .Done, target: <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">self</span>, action: <span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">MyController</span>.functionToCall)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; <span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">func</span> functionToCall(button: <span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">UIBarButtonItem</span>) {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>// Do stuff</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">&nbsp; &nbsp; }</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">}</div></div><div class=""><br class=""></div><br class=""><div apple-content-edited="true" class="">
--<br class="">Jorge Bernal |&nbsp;<a href="mailto:jbernal@gmail.com" class="">jbernal@gmail.com</a> |&nbsp;<a href="mailto:jorge@automattic.com" class="">jorge@automattic.com</a><br class="">Mobile Engineer @ Automattic&nbsp;| <a href="http://automattic.com/" class="">http://automattic.com/</a><br class=""><br class=""><a href="http://koke.me/" class="">http://koke.me/</a> |&nbsp;<a href="http://jorgebernal.es/" class="">http://jorgebernal.es/</a> |&nbsp;<a href="http://twitter.com/koke" class="">http://twitter.com/koke</a>

</div>

<br class=""></body></html>