Let's take a look at a theoretical example:
protocol Text {} //Protocol in the external Framework
extension String: Text {} //Implementing the protocol in the app
protocol A { //Another protocol in the external Framework
var text: Text { get }
}
class B { //Existing class in the app, I'd like to pass where Framework expects protocol A
let text: String = ""
}
I tried doing this:
//ERROR: Type 'B' does not conform to protocol 'A'
extension B: A {}
Trying to use solve
function in XCode does the following:
extension B: A {
//ERROR: Invalid redeclaration of 'text'
var text: Text {
...
}
}
What else can I do? I'd like to avoid changing variable name in the class B
, as it's already wideley used and would be a big refactor.
What I ended up with is some kind of a wrapper:
//1) Wrapper
class C: A {
let b: B
var text: Text {
return b.text
}
init(b: B) {
self.b = b
}
}
extension B {
var wrapper: A {
return C(b: self)
}
}
and now everywhere where external Framwork expects type A
, instead of passing b
I pass b.wrapper
.
Is there any other way to achieve same thing? Creating wrapper doesn't seem like an ideal solution to me.