Home > OS >  How to make protocol exportable from a swift library without making it public?
How to make protocol exportable from a swift library without making it public?

Time:03-05

in my swift library I have a simple protocol like this:

    protocol DebugPrintable {
        func debug() -> String
    }

I would like to export it so that users of the library can refer to the protocol, but that would turn it into a public protocol:

    public protocol DebugPrintable {
        func debug() -> String
    }

... which forces all uses of protocol to be public - which is unnecessary.

Can I somehow make the protocol public (so that it is exported from library) but not make it force all declared properties to be public?

CodePudding user response:

Here from documentation

You can’t set a protocol requirement to a different access level than the protocol it supports. This ensures that all of the protocol’s requirements will be visible on any type that adopts the protocol.

If you define a public protocol, the protocol’s requirements require a public access level for those requirements when they’re implemented. This behavior is different from other types, where a public type definition implies an access level of internal for the type’s members.

https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

I think you should move your protected properties to another protocol to satisfy above thing.

  • Related