2017-02-17 47 views
0

enter image description here無法轉換類型「[MyProtocol]」的值與預期的參數類型「INOUT _」

我試圖找出是什麼讓這發生了,但我失敗了,有什麼問題嗎?

確實有其他人遇到這樣的錯誤?

我怎樣才能爲這樣做,我需要幫助

這是我的代碼:

protocol MyProtocol { 

} 

struct MyStruct: MyProtocol { 

} 


let structs = [MyStruct(), MyStruct()] 

var protocols = [MyProtocol]() 

protocols = structs // it's ok 

protocols += structs // this got an error 

回答

2

有編譯器魔力,在這條線發生:

protocols = structs 

其循環通過結構,將每一個裝入一個協議容器,然後完成任務。它本質上執行此操作:

protocols = structs.map{ $0 as MyProtocol } 

或等價:

protocols = structs as [MyProtocol] 

這個編譯器魔法沒有申請+=操作。你可以自己做,但:

protocols += structs as [MyProtocol] 
相關問題