How to find the index of an item in a multidimensional array swiftily?

后端 未结 2 1620
一生所求
一生所求 2020-12-29 11:06

Let\'s say I have this array:

let a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Now I want something like this:

public func indices         


        
2条回答
  •  不知归路
    2020-12-29 11:34

    Version accepting a closure, similar to index(where:), so there it is usable on the array of any elements, not only Equatable

    extension Array where Element : Collection, Element.Index == Int {
      func indices(where predicate: (Element.Iterator.Element) -> Bool) -> (Int, Int)? {
        for (i, row) in self.enumerated() {
          if let j = row.index(where: predicate) {
            return (i, j)
          }
        }
        return nil
      }
    }
    

    Use like this:

    let testArray = [[1,2,3], [4,5,6], [7,8]]
    
    let testNumber = 6
    
    print(testArray.indices(of: testNumber))
    print(testArray.indices{$0 == testNumber})
    
    Optional((1, 2))
    Optional((1, 2))
    

    Also, it can be used with IndexPath:

    extension Array where Element : Collection, Element.Index == Int {
      func indexPath(where predicate: (Element.Iterator.Element) -> Bool) -> IndexPath? {
        for (i, row) in self.enumerated() {
          if let j = row.index(where: predicate) {
            return IndexPath(indexes: [i, j])
          }
        }
        return nil
      }
    }
    

提交回复
热议问题