Arrays 协议扩展错误数组

Arrays 协议扩展错误数组,arrays,swift,inheritance,Arrays,Swift,Inheritance,我在苹果论坛上写一些代码。一切似乎都很好,但我总是犯两个错误。有人请帮忙。下面是代码,后面是错误 protocol Container{ associatedtype ItemType mutating func append(item: ItemType) var count:Int{get} subscript(i:Int)->ItemType{get} } extension Array: Container {} func checkToSeeIfItemsEqual<C

我在苹果论坛上写一些代码。一切似乎都很好,但我总是犯两个错误。有人请帮忙。下面是代码,后面是错误

protocol Container{
associatedtype ItemType
mutating func append(item: ItemType)
var count:Int{get}
subscript(i:Int)->ItemType{get}
}

extension Array: Container {}

func checkToSeeIfItemsEqual<C1:Container, C2:Container>(container1:C1, container2:C2) -> Bool where C1.ItemType == C2.ItemType, C1.ItemType:Equatable{

if container1.count != container2.count{
    return false
}
for i in 0..<container1.count{
    if container1[i] != container2[i]{
        return false
    }
}
return true
}

var damnArray = [1, 2, 4]
var damnArray2 = [1, 2, 4]
let theBool = checkToSeeIfItemsEqual(container1: damnArray, container2: damnArray2)
print(theBool)
协议容器{
associatedtype项目类型
变异函数追加(项:ItemType)
变量计数:Int{get}
下标(i:Int)->ItemType{get}
}
扩展数组:容器{}
func checkToSeeIfItemsEqual(container1:C1,container2:C2)->Bool其中C1.ItemType==C2.ItemType,C1.ItemType:equalable{
如果container1.count!=container2.count{
返回错误
}

因为0..Any中的i是不相等的,所以不能使用任何数组调用checkToSeeIfItemsEqual

我不确定您到底想做什么,但我认为您可能误解了Equalable的工作原理。类型必须是已知的,并且在表达式的两侧都相同。例如,
Bool==Bool
。如果您有一个数组,您无法知道数组元素的类型,因此无法比较它们

如果要比较相同可等分类型的两个数组,可以执行以下操作:

func arraysAreEqual<ElementType: Equatable>(firstArray: [ElementType], secondArray: [ElementType]) -> Bool {

    guard firstArray.count == secondArray.count else {
        return false
    }

    for i in 0..<firstArray.count{
        if firstArray[i] != secondArray[i]{
            return false
        }
    }
    return true
}
func arraysarequal(第一个数组:[ElementType],第二个数组:[ElementType])->Bool{
guard firstArray.count==secondArray.count else{
返回错误
}

对于0中的i..您的append方法定义稍有不同。您需要添加下划线:mutating func append(uItem:ItemType)。但还有一点是错误的…我让它工作了,没有为你的数组使用任何类型的。我想当尝试查看“any”是否相等时,它可能会崩溃。好吧,我更新了代码,但仍然得到了与所需追加相同的第一个错误。O nvm我找到了答案