Swift Array.insert泛型

Arb*_*tur 5 arrays generics swift

func getIndex<T: Equatable>(valueToFind: T) -> Int? {...}

mutating func replaceObjectWithObject<T: Equatable>(obj1: T, obj2: T) {
    if let index = self.getIndex(obj1) {
        self.removeAtIndex(index)
        self.insert(obj2, atIndex: index)           // Error here: 'T' is not convertible to 'T'
    }
}
Run Code Online (Sandbox Code Playgroud)

我有这个函数,假设用另一个元素替换元素.但我不是很熟悉,Generics也不知道为什么这不起作用.请帮忙.

如果我从变异函数中删除Equatable,则错误消息会跳转到该func中的第一行,如果我然后用func替换它,find()它会给出与第3行相同的错误.

NRi*_*itH 0

您是如何宣布Array延期的?问题是您的泛型函数需要类型的参数Equatable,但是当您声明数组时,您指定了Equatable类的特定实现,例如String。AT不是String没有演员表的 a。