I have the following code:
protocol TestProtocol {
var string: String {get}
}
struct TestStructOne : TestProtocol {
let string = "test string"
var stringTwo = "test string three"
}
struct TestStructTwo : TestProtocol {
let string = "test string two"
}
var testStructOnes = [TestStructOne(), TestStructOne(), TestStructOne()]
// works
var protocolArrayOne: [TestProtocol] = [TestProtocol]()
for testStruct in testStructOnes.filter({ $0.stringTwo == "test string three" }) {
protocolArrayOne.append(testStruct)
}
// does not work, cannot copnvert value of type '[TestStructOne]' to specified type '[TestProtocol]'
var protocolArrayTwo: [TestProtocol] = testStructOnes.filter({ $0.stringTwo == "test string three" })
I don't understand why the last line doesn't work. Can anybody fill me in? I don't see how it is different than iterating through the array and manually adding each element.