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: Andreas Mayer <email@hidden>
- Date: Wed, 25 Nov 2015 07:54:57 +0100
> if I define an internal class which implements a public protocol, who is able to call the methods in that protocol? Anyone (it’s a public protocol), only classes in the same framework or source file (the class is internal)?
If you declare it internal it's internal. Adopting a public protocol doesn't change that.
> In this instance I have an internal class implementing the CBCentralManager protocol and yet if I supply this as a delegate, CoreBluetooth is able to call those internal methods just fine. I suspect in this case it’s because we’re actually in the ObjC world here (the protocol extends NSObjectProtocol) and in the ObjC world if you can find it, you can call it.
Access levels are applied at compile time only. Once you get it to compile, the runtime will not care. Also, there are no restrictions in respect to passing parameters. As long as you have access to an entity, you can pass it to someone else; that's your responsibility.
> The compiler messages don’t really help to figure out what the rules are, if I have this
Swift error messages are still confusing sometimes. But in this case the compiler is pretty explicit about the problem.
> The last one I don’t get at all, why should making the class private stop the methods being implicitly @objc?
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."
It is not explained why, but I suspect it's because @objc adds the declaration to the dynamic runtime and makes it introspectable. If you explicitly mark something as private, you might not want that. (And there's no @not-objc modifier.)
Andreas
_______________________________________________
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