Generics Swift泛型和下标值与K不相同

Generics Swift泛型和下标值与K不相同,generics,swift,subscript,Generics,Swift,Subscript,我得到一个编译错误,因为编译器认为元组中的“键”与泛型数组的类型不同。知道为什么吗 class MyClass <K: Hashable, V> { let keys = [K]() subscript(index: Int) -> (key: K, value: V) { set(newValue) { // also tried newValue.key! keys[index] = newValue.key

我得到一个编译错误,因为编译器认为元组中的“键”与泛型数组的类型不同。知道为什么吗

class MyClass <K: Hashable, V> {

   let keys = [K]()

   subscript(index: Int) -> (key: K, value: V) {
      set(newValue) {
         // also tried newValue.key!
         keys[index] = newValue.key
      }
   }
}
class-MyClass{
设keys=[K]()
下标(索引:Int)->(键:K,值:V){
设置(新值){
//还尝试了newValue.key!
keys[index]=newValue.key
}
}
}

当数组声明为不可变数组时,您试图设置数组
键的值<代码>键
数组应声明为:

var keys = [K]() // var declaration makes it mutable

顺便问一下,为什么在setter中有
return
语句?它应该也会给你一个编译错误?@Keenle很抱歉,我的问题中有一个输入错误,实际代码是正确的,就是这样。使数组成为最终数组,而不是使用let