我有一个数组[a,b,a,c,d]
我可以通过a
array.index(where:{$0 is a})
的索引
如何获取第二项a
的索引?
答案 0 :(得分:2)
以下是一些例子:
let xs = ["a", "b", "a", "c", "d"]
let ys = xs.indices.filter { xs[$0] == "a" }
print(ys) // [0, 2]
// or
let zs = xs.enumerated().flatMap { $0.element == "a" ? $0.offset : nil }
print(zs) // [0, 2]