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 08:16:08 +0800
> On 25 Nov 2015, at 00:59, Andreas Mayer <email@hidden> wrote:
>
>
>> Am 24.11.2015 um 15:10 schrieb Roland King <email@hidden>:
>>
>> in this class, centralManagerDidUpdateState() is an internal function however the compiler is happy that the class satisfies CBCentralManagerDelegate even though that protocol is public and centralManagerDidUpdateState() is also public. How can this be, how can an internal function make a class conform to a public method on a public protocol. That function is required in the protocol.
>
> Maybe I'm misunderstanding something, but I'm not sure why you think this should be a problem?
>
> You can always make things more restrictive.
>
> It's the other way around that is not allowed. Say, using an internal protocol to define a public class - that wouldn't work, since the user of the class might not have access to the internal protocol.
ok to this and Quincey’s email also. I can see the argument about making things more restrictive, not less. In that case then, 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)?
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.
The compiler messages don’t really help to figure out what the rules are, if I have this
<access specifier> Class Foo : NSObject, CBCentralManagerDelegate
{
func centralManagerDidUpdateState(.. ) .. // required
func centralManager(central:CBCentralManager, didConnectPeripheral:CBPeripheral) //optional
}
if access specifier is public I get the message, for both of those
centralManagerDidUpdateState() must be declared public because it matches a requirement in the public protocol CBCentralManagerDelegate
ditto for centralManager(central:CBCentralManager, didConnectPeripheral:CBPeripheral)
if access specifier is internal I get nothing, all compiles fine
if access specifier is private I get a very odd combo
centralManagerDidUpdateState has no errors or warnings at all
centralManager(central:CBCentralManager, didConnectPeripheral:CBPeripheral) gives the message “Non-@objc method centralManager(central:CBCentralManager, didConnectPeripheral:CBPeripheral) cannot satisfy optional requirement of @objc protocol CBCentralManagerDelegate’
I find this confusing because the protocol is public, the error message when making the class public makes sense, it’s telling me that public protocols need public methods. The last one I don’t get at all, why should making the class private stop the methods being implicitly @objc?
_______________________________________________
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