<div dir="ltr">I usually mark my target-action methods like so:<div><br></div><div>private dynamic func didTapNext(sender: UIButton)</div><div><br></div><div>&quot;dynamic&quot; makes the compiler generate code for dynamic dispatch, that is runtime message passing, like objc. The &quot;private&quot; part doesn&#39;t change that, it just reduces clutter.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-07-22 15:18 GMT+02:00 Tod Cunningham via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">What’s the best practice around using @objc on private methods?  While they can be used together, should their use be avoided or embraced?<br>
<br>
Thanks,<br>
Tod<br>
<br>
From: Zhao Xin &lt;<a href="mailto:owenzx@gmail.com">owenzx@gmail.com</a>&gt;<br>
Date: Thursday, July 21, 2016 at 10:42 PM<br>
To: Josh Parmenter &lt;<a href="mailto:jparmenter@vectorform.com">jparmenter@vectorform.com</a>&gt;<br>
Cc: Tod Cunningham &lt;<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>&gt;, &quot;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&quot; &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;<br>
<span class="">Subject: Re: [swift-users] @objc and private methods<br>
<br>
</span><span class="">My previous reply was not correct.<br>
I think @objc makes the function an Objective-C function, so the private is no longer making the function private in Swift way, but in Objective-C way. In C++,  sub-class can call super-class&#39;s private method. What you have to do is to override the function.<br>
<br>
​@objc makes the function available to Objective-C, but not only Objective-C. So the function works in both Objective-C and Swift. In Swift, the private is different from other languages, its scope is the surrounding ​file. That is why the override doesn&#39;t work if both classes are separated in different files.<br>
<br>
If the two classes are in the same file, below code also works.<br>
<br>
<br>
class Test {<br>
<br>
<br>
<br>
    private func somefunc() {<br>
<br>
<br>
<br>
        print( &quot;hello 1&quot; )<br>
<br>
<br>
<br>
    }<br>
<br>
<br>
<br>
}<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
class Test2: Test {<br>
<br>
<br>
<br>
    override private func somefunc() {<br>
<br>
<br>
<br>
        print( &quot;hello 2&quot; )<br>
<br>
<br>
<br>
    }<br>
<br>
<br>
<br>
}<br>
<br>
So the only problem is with @objc and put the two classes into different files, you can&#39;t override the function as it is over-scoped in Swift. However, you can&#39;t use it in the same name either, as Objective-C will find the function names are conflicts.<br>
<br>
So the resolution is either to use it as it is, or to file a bug to eliminate using @objc and private together as there is no private access level in Objective-C.<br>
<br>
Zhaoxin<br>
<br>
</span><span class="">On Fri, Jul 22, 2016 at 2:22 AM, Josh Parmenter &lt;<a href="mailto:jparmenter@vectorform.com">jparmenter@vectorform.com</a>&lt;mailto:<a href="mailto:jparmenter@vectorform.com">jparmenter@vectorform.com</a>&gt;&gt; wrote:<br>
Is this a bug? I think this is more a question of what is best practice.<br>
Best,<br>
Josh<br>
<br>
</span><span class="">On Jul 21, 2016, at 10:48 AM, Zhao Xin via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&gt;&gt; wrote:<br>
<br>
Then you should file a bug.<br>
<br>
Zhaoxin<br>
<br>
</span><span class="">On Fri, Jul 22, 2016 at 1:03 AM, Tod Cunningham &lt;<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>&lt;mailto:<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>&gt;&lt;mailto:<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>&lt;mailto:<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>&gt;&gt;&gt; wrote:<br>
If Test and Test2 are in separate files, swift doesn’t allow you to do the override as it “can’t see” the definition of the private method in Test.<br>
<br>
<br>
</span>From: Zhao Xin &lt;<a href="mailto:owenzx@gmail.com">owenzx@gmail.com</a>&lt;mailto:<a href="mailto:owenzx@gmail.com">owenzx@gmail.com</a>&gt;&lt;mailto:<a href="mailto:owenzx@gmail.com">owenzx@gmail.com</a>&lt;mailto:<a href="mailto:owenzx@gmail.com">owenzx@gmail.com</a>&gt;&gt;&gt;<br>
<span class="">Date: Thursday, July 21, 2016 at 11:06 AM<br>
</span>To: Tod Cunningham &lt;<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>&lt;mailto:<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>&gt;&lt;mailto:<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>&lt;mailto:<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>&gt;&gt;&gt;<br>
Cc: &quot;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&gt;&quot; &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&gt;&gt;<br>
<span class="">Subject: Re: [swift-users] @objc and private methods<br>
<br>
I think @objc makes the function an Objective-C function, so the private is no longer making the function private in Swift way, but in Objective-C way. In C++,  sub-class can call super-class&#39;s private method. What you have to do is to override the function.<br>
<br>
<br>
class Test {<br>
<br>
    @objc private func somefunc() {<br>
<br>
        print( &quot;hello 1&quot; )<br>
<br>
    }<br>
<br>
}<br>
<br>
<br>
<br>
class Test2: Test {<br>
<br>
    @objc override private func somefunc() {<br>
<br>
        print( &quot;hello 2&quot; )<br>
<br>
    }<br>
<br>
}<br>
<br>
<br>
<br>
Zhaoxin<br>
<br>
<br>
<br>
<br>
</span><div><div class="h5">On Thu, Jul 21, 2016 at 10:25 PM, Tod Cunningham via swift-users &lt;<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&gt;&gt;&gt; wrote:<br>
I wanted to get thoughts on the use of @objc and private methods.  Should this usage be avoided or leveraged?<br>
<br>
I have seen some code where things like UIButton and Gesture recognizer handlers are defined as being private.  I would have thought this would result in a compiler error given objective-c doesn’t support private accessors.  However, it compiles and works as expected (kind of).<br>
<br>
One plus for defining the handlers as private is they aren’t visible to swift outside of the file they are defined in.  The problem is you can run into some issues with inheritance if you define the same handler in a derived class.  Take the following simple example:<br>
<br>
class Test {<br>
    @objc private func somefunc() {<br>
        print( &quot;hello 1&quot; )<br>
    }<br>
}<br>
<br>
-- in another file –<br>
<br>
class Test2: Test {<br>
    @objc private func somefunc() {<br>
        print( &quot;hello 2&quot; )<br>
    }<br>
}<br>
<br>
This will result in the following compiler error:<br>
<br>
Method &#39;somefunc()&#39; with Objective-C selector &#39;somefunc&#39; conflicts with method &#39;somefunc()&#39; from superclass &#39;Test&#39; with the same Objective-C selector<br>
<br>
That error makes sense in an Objective-C context as Test2 has two different methods defined with the same objective-c selector (somefunc).  Swift can tell them apart as they are private to each class, but Obj-C can’t tell them apart as they have the same selector thus the error.<br>
<br>
One workaround would be to give the method a different Objective-C names such as @objc(test2_somefunc); however, that could really lead to some unexpected results depending on what selector is used to call somefunc.  Test2 would need to “register” a different selector for somefunc then Test does for whatever is calling it on the Objective-C side (yuck!).<br>
<br>
This can of course be solved by not making the method private which allows Test2 to explicitly override somefunc.<br>
<br>
class Test {<br>
   @objc func somefunc() {<br>
       print( &quot;hello 1&quot; )<br>
   }<br>
}<br>
<br>
-- in another file –<br>
<br>
class Test2: Test {<br>
   @objc override func somefunc() {<br>
       print( &quot;hello 2&quot; )<br>
   }<br>
}<br>
<br>
I would say that when declaring @objc methods they shouldn’t be private and should have the same (public/internal) access method as their containing class.  What’s your thoughts?  Why does swift allow a method to be declared as both @objc and private?<br>
<br>
Thanks,<br>
<br>
<br>
<br>
_______________________________________________<br>
swift-users mailing list<br>
</div></div><a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&gt;&gt;<br>
<span class=""><a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br>
<br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&lt;mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>&gt;&gt;<br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br>
_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
</span><a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
</blockquote></div><br></div>