Hi
I've been trying to work around an issue whereby a have a public protocol with some public properties but also an internal one. For example:
public protocol ServiceTypeProtocol
{
var type : String {get}
var endpointUri: URL { get }
var method: String { get }
}And the implementation of the protocol would be somethig like this:
public struct Action : ServiceTypeProtocol {
public var type: String
public var endpointUri: URL
internal var method: String
init(_ type: String, endpointUri : String)
{
self.type = type
self.endpointUri = endpointUri
self.method = type == "cloud" ? "delete" : "patch"
}
}So method is only exposed internally from the Action struct. However the complier throws this error:
"Property 'method' must be declared public because it matches a requirement in public protocol 'ServiceTypeProtocol'"
If I change the protocol to the following, I end up with this error:
'internal' modifier cannot be used in protocols
public protocol ServiceTypeProtocol {
var type : String {get}
var endpointUri: URL { get }
internal var method: String { get }
}I removed the method from the protocol and the Action struct and created an extension of the protocol this this:
internal extension ServiceTypeProtocol {
var method : String
}But you end up with the error:
Extensions may not contain stored properties
Then I tried this as well, at least it did compile...
internal extension ServiceTypeProtocol {
var method : String {
get { return "foo" }
set { self.method = newValue }
}
}But the set operation repeats itself unti this error occurs:
EXC_BAD_ACCESS (code=2, address=0x7ffeeb937ff8)
So what approach have others used to keep a property internal still getable and only setable in the init?
Many thanks