gpt4 book ai didi

Swift 泛型和扩展需要锻炼

转载 作者:可可西里 更新时间:2023-11-01 00:52:54 25 4
gpt4 key购买 nike

<分区>

我目前正在快速学习。在学习的过程中,我坚持使用泛型。我正在解决一个简单的问题 -> 返回数组中指定元素的索引

import UIKit

extension Array
{

func indexOfLetter<T:Equatable>(item:T) -> Int
{
var i = 0
for (index, value) in enumerate(self)
{
if value == item
{
return i
}
i++
}

return -1;
}
}


var arrayOfItems = ["A","B"]
arrayOfItems.indexOfLetter("A")

在这段代码中我遇到了错误

Can not compare two operands using == operator which are of type T.

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com