Re: Swift - internal class conforming to public protocol
Re: Swift - internal class conforming to public protocol
- Subject: Re: Swift - internal class conforming to public protocol
- From: Roland King <email@hidden>
- Date: Wed, 25 Nov 2015 16:39:12 +0800
> On 25 Nov 2015, at 16:30, Andreas Mayer <email@hidden> wrote:
>
>
>> Am 25.11.2015 um 08:56 schrieb Quincey Morris <email@hidden>:
>>
>>> That's explained in "Using Swift with Cocoa and Objective-C":
>>>
>>> "The compiler does not automatically insert the @objc attribute for declarations marked with the private access-level modifier.”
>>
>> That can’t be the full explanation, because the other private method doesn’t produce an error, Roland said.
>
> I can't replicate that behavior.
>
> This doesn't work:
>
> // Roland's protocol must be marked @objc since it has optional requirements.
>
> @objc public protocol PublicProtocol {
> func someFunction()
> // ...
> }
>
> private class SomeClass: NSObject, PublicProtocol {
> func someFunction() {
> // implementation
> }
> }
>
> Type 'SomeClass' does not conform to protocol 'PublicProtocol'
> Fix-it: Candidate is not '@objc', but protocol requires it
>
I believe that is what I saw when I made the class private, it required an explicit @objc, in my case it said it required @objc because the method was optional.
So after this discussion, which was as-usual helpful, I see why you can use a protocol in a class with lesser access rights. The last question I had was, if I define a private class to conform to a public protocol, and then arrange in the same source file a public class which creates an instance of the private one and returns it as a protocol type, can I then use it. And the answer was yes I can. I had an entirely private Bar implementing a public protocol Foo and a public struct Baz in the same source file as Bar which had a returnAsFoo() method which returned a Bar .. as a Foo. I was able to use that in a playground even though Bar was entirely inaccessible from the playground.
So this all makes more sense now.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden