【发布时间】:2018-04-13 15:44:50
【问题描述】:
我想检查数组中元素的索引:
var arr: [Any] = ["st","er","gh", 2, 5,"jk", 78 ]
print(arr.index(of: 2))
但我得到一个错误:
error: cannot invoke 'index' with an argument list of type '(of: Any)'
note: expected an argument list of type '(of: Self.Element)'
为什么控制台会抛出这个问题以及如何解决它?
【问题讨论】:
-
要使用
index(of:)必须采用Equatable协议。 -
尽量避免这样的异构数组。与他们一起工作是一个巨大的痛苦。
-
哈我想知道使用这种类型的数组是否“可以接受”,但现在我知道它不是。感谢您的建议:)