[英]In argument type '[ItemA]', 'ItemA' does not conform to expected type 'Sortable'
我使用Swift遇到了一個奇怪的錯誤,但我似乎無法找到問題。 我認為不應該拋出錯誤,我已經在操場上用下面的代碼驗證了這個問題。
protocol Sortable {
}
protocol ItemA: Sortable {
}
func sortItems<T: Sortable>(items: [T]) -> [T] {
// do the sorting here
return items
}
let list: [ItemA] = []
sortItems(items: list)
您無法傳遞從當前Swift版本(4.1)中的受約束協議繼承的其他協議。
如果你使ItemA
成為struct
, class
或enum
,它將起作用。
要么
如果您要將sortItems
實現更改為簡單地將Sortable
作為這樣的參數,那么您可以使用繼承自Sortable
其他協議,但是您將丟失有關該類型的信息。
func sortItems(items: [Sortable]) -> [Sortable] {
// do the sorting here
return items
}
您可以在此處找到有關此問題的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.