<div dir="ltr">Jorge's second suggestion seems like a good one - it definitely makes more sense to lay it out that way than the current usage of strings which makes me cry a little bit..<div><br></div><div>It would be nice to eliminate the need for "myFunction"/"myFunction:".</div><div><br></div><div>Chris</div><div><br><div class="gmail_quote"><div dir="ltr">On Sat, 5 Dec 2015 at 16:21 Jorge Bernal <<a href="mailto:me@koke.me">me@koke.me</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>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></div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On 04 Dec 2015, at 19:49, Chris Byatt <<a href="mailto:byatt.chris@gmail.com" target="_blank">byatt.chris@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div>We should be able to do:</div><div><br></div><div><pre 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"><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">UIBarButtonItem</span><span style="margin:0px;padding:0px;border:0px">(</span><span style="margin:0px;padding:0px;border:0px">title</span><span style="margin:0px;padding:0px;border:0px">:</span><span style="margin:0px;padding:0px;border:0px"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(128,0,0)">"Press"</span><span style="margin:0px;padding:0px;border:0px">,</span><span style="margin:0px;padding:0px;border:0px"> style</span><span style="margin:0px;padding:0px;border:0px">:</span><span style="margin:0px;padding:0px;border:0px"> </span><span style="margin:0px;padding:0px;border:0px">.</span><span style="margin:0px;padding:0px;border:0px;color:rgb(43,145,175)">Done</span><span style="margin:0px;padding:0px;border:0px">,</span><span style="margin:0px;padding:0px;border:0px"> target</span><span style="margin:0px;padding:0px;border:0px">:</span><span style="margin:0px;padding:0px;border:0px"> </span><span style="margin:0px;padding:0px;border:0px;color:rgb(0,0,139)">self</span><span style="margin:0px;padding:0px;border:0px">,</span><span style="margin:0px;padding:0px;border:0px"> action</span><span style="margin:0px;padding:0px;border:0px">:</span><span style="margin:0px;padding:0px;border:0px"> </span><span style="margin:0px;padding:0px;border:0px">{</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px;color:rgb(128,128,128)">// Do Something</span><span style="margin:0px;padding:0px;border:0px">
</span><span style="margin:0px;padding:0px;border:0px">})</span></code></pre></div></div></div></blockquote></div></div><div style="word-wrap:break-word"><div>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>I see a couple solutions to this:</div><div><br></div><div>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>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><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#bb2ca2">class</span> MyController: <span style="color:#703daa">UIViewController</span> {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">override</span> <span style="color:#bb2ca2">func</span> viewDidLoad() {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">super</span>.<span style="color:#3d1d81">viewDidLoad</span>()</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">self</span>.<span style="color:#703daa">navigationItem</span>.rightBarButtonItem = <span style="color:#703daa">UIBarButtonItem</span>(title: <span style="color:#d12f1b">"Press"</span>, style: .Done, target: <span style="color:#bb2ca2">self</span>, action: <span style="color:#4f8187">MyController</span>.functionToCall)</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">func</span> functionToCall(button: <span style="color:#703daa">UIBarButtonItem</span>) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,132,0)"><span style="color:#000000"> </span>// Do stuff</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</div></div><div><br></div><br><div>
--<br>Jorge Bernal | <a href="mailto:jbernal@gmail.com" target="_blank">jbernal@gmail.com</a> | <a href="mailto:jorge@automattic.com" target="_blank">jorge@automattic.com</a><br>Mobile Engineer @ Automattic | <a href="http://automattic.com/" target="_blank">http://automattic.com/</a><br><br><a href="http://koke.me/" target="_blank">http://koke.me/</a> | <a href="http://jorgebernal.es/" target="_blank">http://jorgebernal.es/</a> | <a href="http://twitter.com/koke" target="_blank">http://twitter.com/koke</a>
</div>
<br></div></blockquote></div></div></div>