Reputation: 1469
I am trying to work with protocols across Pods. I have then simplified by problem here so I can ask a question on StackOverflow.
In my problem TestClass does not know anything about SecondProtocol (as it is in a separate pod).
public protocol FirstProtocol {
func get(data: String) -> String
}
public protocol SecondProtocol {
func get(data: String) -> String
}
class TestClass {
func get(data: String) -> String {
return "Rest data"
}
public init() { }
}
extension TestClass : FirstProtocol {}
let myTest : FirstProtocol?
myTest = TestClass() as FirstProtocol
let secondTest: SecondProtocol?
secondTest = myTest as! SecondProtocol
So the last line causes a crash in Swift. In a sense, I understand why. However secondTest is instantiated from the main program rather than the Pod secondTest, so SecondTest knows nothing about SecondProtocol. Yet a second pod expects an input of Second Protocol.
So how can I get secondTest to cast to SecondProtocol?
Upvotes: 0
Views: 438
Reputation: 10054
All you're missing is to make TestClass
conform to SecondProtocol
, just like you did for FirstProtocol
:
extension TestClass : SecondProtocol {}
You can specify protocol conformances for classes, structs, and enums that are defined in other modules, such as in Foundation or UIKit as well as in third party dependencies.
Upvotes: 4