簡體   English   中英

在參數類型'[ItemA]'中,'ItemA'不符合預期類型'Sortable'

[英]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成為structclassenum ,它將起作用。

要么

如果您要將sortItems實現更改為簡單地將Sortable作為這樣的參數,那么您可以使用繼承自Sortable其他協議,但是您將丟失有關該類型的信息。

func sortItems(items: [Sortable]) -> [Sortable] {
    // do the sorting here
    return items
}

您可以在此處找到有關此問題的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM