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

后端 未结 2 1599
一生所求
一生所求 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:36

    You can simplify your code slightly with enumerate() and indexOf(). Also the function should return an optional tuple because the element might not be present in the "matrix". Finally, you can make it generic:

    func indicesOf(x: T, array: [[T]]) -> (Int, Int)? {
        for (i, row) in array.enumerate() {
            if let j = row.indexOf(x) {
                return (i, j)
            }
        }
        return nil
    }
    

    You can also make it an extension for a nested Array of Equatable elements:

    extension Array where Element : CollectionType,
        Element.Generator.Element : Equatable, Element.Index == Int {
        func indicesOf(x: Element.Generator.Element) -> (Int, Int)? {
            for (i, row) in self.enumerate() {
                if let j = row.indexOf(x) {
                    return (i, j)
                }
            }
            return nil
        }
    }
    
    if let (i, j) = a.indicesOf(7) {
        print(i, j)
    }
    

    Swift 3:

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

提交回复
热议问题