<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">My previous reply was not correct.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><font face="georgia, serif">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's private method. What you have to do is to override the function.</font></blockquote><div><br></div><div class="gmail_default" style="font-family:georgia,serif">@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't work if both classes are separated in different files. </div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">If the two classes are in the same file, below code also works.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif"><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">class</span><span style=""> Test {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">private</span><span style=""> </span><span style="color:rgb(187,44,162)">func</span><span style=""> somefunc() {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(61,29,129)">print</span><span style="">( </span><span style="color:rgb(209,47,27)">"hello 1"</span><span style=""> )</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">}</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""></span><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">class</span><span style=""> Test2: </span><span style="color:rgb(79,129,135)">Test</span><span style=""> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(187,44,162)">override</span><span style=""> </span><span style="color:rgb(187,44,162)">private</span><span style=""> </span><span style="color:rgb(187,44,162)">func</span><span style=""> somefunc() {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> </span><span style="color:rgb(61,29,129)">print</span><span style="">( </span><span style="color:rgb(209,47,27)">"hello 2"</span><span style=""> )</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style=""> }</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style=""> </span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="">}</span></p></div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">So the only problem is with @objc and put the two classes into different files, you can't override the function as it is over-scoped in Swift. However, you can't use it in the same name either, as Objective-C will find the function names are conflicts.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">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.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">Zhaoxin</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 22, 2016 at 2:22 AM, Josh Parmenter <span dir="ltr"><<a href="mailto:jparmenter@vectorform.com" target="_blank">jparmenter@vectorform.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Is this a bug? I think this is more a question of what is best practice.<br>
Best,<br>
Josh<br>
<span class=""><br>
On Jul 21, 2016, at 10:48 AM, Zhao Xin via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>>> 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 <<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a><mailto:<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>>> 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 <<a href="mailto:owenzx@gmail.com">owenzx@gmail.com</a><mailto:<a href="mailto:owenzx@gmail.com">owenzx@gmail.com</a>>><br>
<span class="">Date: Thursday, July 21, 2016 at 11:06 AM<br>
</span>To: Tod Cunningham <<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a><mailto:<a href="mailto:tcunningham@vectorform.com">tcunningham@vectorform.com</a>>><br>
Cc: "<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>>" <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>>><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'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( "hello 1" )<br>
<br>
}<br>
<br>
}<br>
<br>
<br>
<br>
class Test2: Test {<br>
<br>
@objc override private func somefunc() {<br>
<br>
print( "hello 2" )<br>
<br>
}<br>
<br>
}<br>
<br>
<br>
<br>
Zhaoxin<br>
<br>
<br>
<br>
<br>
<br>
</span><div><div class="h5">On Thu, Jul 21, 2016 at 10:25 PM, Tod Cunningham via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>>>> 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( "hello 1" )<br>
}<br>
}<br>
<br>
-- in another file –<br>
<br>
class Test2: Test {<br>
@objc private func somefunc() {<br>
print( "hello 2" )<br>
}<br>
}<br>
<br>
This will result in the following compiler error:<br>
<br>
Method 'somefunc()' with Objective-C selector 'somefunc' conflicts with method 'somefunc()' from superclass 'Test' 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( "hello 1" )<br>
}<br>
}<br>
<br>
-- in another file –<br>
<br>
class Test2: Test {<br>
@objc override func somefunc() {<br>
print( "hello 2" )<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><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>>><br>
<div class="HOEnZb"><div class="h5"><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><mailto:<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>><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>
</div></div></blockquote></div><br></div>