如何获取2D数组的行索引?

时间:2017-06-13 12:27:54

标签: ios arrays swift multidimensional-array swift3

我是Swift的新手,我正在使用2D数组进行比较,一旦条件为真,我需要得到行索引,但我得到的错误状态为cannot invoke index with an argument list of type (of:Array<Float>)

我的代码:

var entryCoordinate: [[Float]] = [[130.6,61.0],[167.5,61.0],[204.5,61.0],[243.6,61.0],[281.16,61.0],[315.3,61.0]]

 for indexs in entryCoordinate
        {
            if indexs[0] == startPathPointX && indexs[1] == startPathPointY
            {
                let pathStartElement = indexs.index(of: indexs)
                print(pathStartElement)
            }

            if indexs[0] == endPathPointX && indexs[1] == endPathPointY
            {
                let pathEndElement = indexs.index(of: indexs)
                print(pathEndElement)
            }
        }

2 个答案:

答案 0 :(得分:0)

从包含startPathPointYendPathPointY的代码中,你需要比较2D数组中的第二个对象,但是你要继续比较第一个对象,并且你要使用index(where:)和你的数组这样来得到pathStartElementpathEndElement

if let pathStartElement = entryCoordinate.index(where: { $0[0] == startPathPointX && $0[1] == startPathPointY }) {
    print(pathStartElement)
}
if let pathEndElement = entryCoordinate.index(where: { $0[0] == endPathPointX && $0[1] == endPathPointY }) {
    print(pathEndElement)
}

答案 1 :(得分:0)

试试这段代码:

let myIndexPathStartElement = self.arrImagetData.index(where: { $0[0] == startPathPointX && $0[1] == startPathPointY })

let myIndexPathEndElement = self.arrImagetData.index(where: { $0[0] == endPathPointX && $0[1] == endPathPointY })