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'
}
}
I have that function which is suppose to replace an element with another element. But Im not very familiar with Generics and don't know why this is not working. Please help.
If I remove the Equatable from the mutating func the error message jumps to the first line in that func and if I then replace that with the func find() that gives me the same error as on line 3.
insertmethod, or at least its signature?Arrayextension, isn't it?Arrayuses T as its generic subtype. It's saying thatT(the generic type for this function) is not convertible toT(the generic type for the array). If you change the method signature to haveUinstead ofTit will be more clear.