ios - 如何将 array.indexOf 与协议(protocol)一起使用?

标签 ios swift algorithm

<分区>

var items = [LiveCellProtocol]() //unsorted array

public class User: LiveCellProtocol {
...
}

let mike = User()
items.append(mike)
items.indexOf(mike) //throws an error

Cannot convert value of type 'LiveCellProtocol' to expected argument type '@noescape (LiveCellProtocol) throws -> Bool'

我该如何解决这个问题?我真的很想使用 .indexOf 方法。有针对这个的解决方法吗?

现在,我正在通过遍历每个项目来破解解决方案,这是线性时间。

最佳答案

为了能够在协议(protocol)中使用 indexOf,它必须是 Equatable。使您的协议(protocol)符合它,您将能够使用它。有关更多信息,请查看 here .

关于ios - 如何将 array.indexOf 与协议(protocol)一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560405/

相关文章:

c++ - 如何控制 double 计算以避免 C++ linux x86_64 中的舍入错误?

ios - 如何使用 NSUserDefaults 设置 bool 值。预期声明错误

swift - AKAudioFile 导出异步路径错误

python-3.x - 我应该在哪里修改广度优先搜索算法以找到 2 个节点之间的最短路径?

ios - 如何在 iOS Swift 3 中允许 CollectionViewCell 文本字段的所有字符均为大写字母且字符限制为 1?

swift - "The file "MyApp.app "couldn' t被打开,因为你在 Xcode 6 Beta 4 中运行应用程序时没有 't have permission to view it"

algorithm - 如果在 IF 语句中有 return 或 throw 语句,可以不使用 ELSE 语句吗?

ios - 如何将水平 UICollectionView 的第一个和最后一个单元格居中?

ios - CGRect相交矩形 : how change the detection to the edge of the picture inside UIImageView

ios - 在编辑模式下更改 UITableViewCell 中红色减号按钮的位置