Swift Array.insert generics

前端 未结 5 1674
盖世英雄少女心
盖世英雄少女心 2020-12-07 03:02
func getIndex(valueToFind: T) -> Int? {...}

mutating func replaceObjectWithObject(obj1: T, obj2: T) {
    if let index =          


        
5条回答
  •  情歌与酒
    2020-12-07 03:05

    What you are trying to do cannot be done using class/struct functions - @Nate Cook has provided a very good solution using a global function.

    By the way the reason why it doesn't work becomes clearer if in your extension methods you replace T with V: they are different types. That also explains why the same error occurs if you remove the dependency from Equatable: the array holds objects of T type, but you are trying to insert a V value.

提交回复
热议问题