【发布时间】:2017-07-29 18:54:52
【问题描述】:
很想知道是否有更好的方法来做到这一点。我有一个包含整数的数组数组。所有整数都是唯一的。我想搜索给定的数字并返回第一个数组的索引。我不在乎包含数字的数组的索引是什么。
这可以满足我的要求,但我是 swift 新手,并认为有更好的方法。
let arr:[[Int]] = [[0], [1], [2, 3], [4], [11, 12, 13, 14, 15, 16], [5], [6], [7], [8], [9], [10]]
var t = -1
for q in arr {
t += 1
if let x = q.index(of: 13) { // <-- looking for the index that contains 13
print ("t: \(t)") // <-- this is what I want. Returns '4'
}
}
【问题讨论】: