【问题标题】:Index out of range error in Swift 3Swift 3 中的索引超出范围错误
【发布时间】:2017-12-07 13:00:44
【问题描述】:

我知道在 SO 中有很多关于这个问题的问题,但我发现对我没有任何用处。

我正在做一个能够识别屏幕上运动的应用。但是 Apple 手势识别器对我来说太精确了,所以我自己做。

它几乎完成并且正在工作。

我想处理多点触控手势(如捏合),我需要获取屏幕上每个手指的方向,为此我想将每个方向包含在一个数组中,以便之后轻松比较它们。但是我得到了索引超出范围的错误,我不知道我错在哪里。我对 swift 很陌生(自 1 个月以来自学),所以它可能很明显,甚至是一个愚蠢的错误......

如果您能在这方面帮助我,我会很高兴。 谢谢 !

这是我的完整代码:

import UIKit

class ViewController: UIViewController {
//@IBOutlet weak var statusLabel: UILabel!

@IBOutlet weak var StatusLabel: UILabel!

var fingers = [String?](repeating: nil, count:10)
var finger1 = [CGFloat]()
var finger2 = [CGFloat]()
var finger3 = [CGFloat]()
var finger4 = [CGFloat]()
var finger5 = [CGFloat]()
var direction: String = ""
var direction1 = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        direction = ""
        direction1 = []
        finger1 = []
        finger2 = []
        finger3 = []
        finger4 = []
        finger5 = []
        for touch in touches{
            let point = touch.location(in: self.view)
            for (index,finger)  in fingers.enumerated() {
                if finger == nil {
                    fingers[index] = String(format: "%p", touch)
                    print("finger \(index+1): x=\(point.x) , y=\(point.y)")
                    break
                }
            }
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        for touch in touches {
            let point = touch.location(in: self.view)
            for (index,finger) in fingers.enumerated() {
                if let finger = finger, finger == String(format: "%p", touch) {
                    switch (index){
                    case 0 :
                        finger1 += [point.x, point.y]
                    case 1 :
                        finger2 += [point.x, point.y]
                    case 2 :
                        finger3 += [point.x, point.y]
                    case 3 :
                        finger4 += [point.x, point.y]
                    case 4 :
                        finger5 += [point.x, point.y]
                    default :
                        break
                    }
                }
            }
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        for touch in touches {
            for (index,finger) in fingers.enumerated() {
                if let finger = finger, finger == String(format: "%p", touch) {
                    fingers[index] = nil
                    break
                }
            }
        }
        if finger1.count != 0 {
            direction1[0] += GestureRecognizer(coordinates: finger1, index: 0)
        }
        if finger2.count != 0 {
            direction1[1] += GestureRecognizer(coordinates: finger2, index: 1)
        }
        if finger3.count != 0 {
            direction1[2] += GestureRecognizer(coordinates: finger3, index: 2)
        }
        if finger4.count != 0 {
            direction1[3] += GestureRecognizer(coordinates: finger4, index: 3)
        }
        if finger5.count != 0 {
            direction1[4] += GestureRecognizer(coordinates: finger5, index: 4)
        }

        print("1 " + direction1[0] + "2 " + direction1[1] + "3 " + direction1[2] + "4 " + direction1[3] + "5 " + direction1[4])
        StatusLabel.text = direction1[1]
    }

    func GestureRecognizer (coordinates: [CGFloat], index: Int) -> String {
        if (coordinates[0] - coordinates[coordinates.count-2]) > 100 && (coordinates[1] - coordinates[coordinates.count-1]) < (-100) {
            print("Vers la gauche et bas")
            direction1[0] = "downleft"
        }
        else if (coordinates[0] - coordinates[coordinates.count-2]) < (-100) && (coordinates[1] - coordinates[coordinates.count-1]) > 100{
            print("Vers la droite et haut")
            direction1[index] = "upright"
        }
        else if (coordinates[0] - coordinates[coordinates.count-2]) < (-100 ) && (coordinates[1] - coordinates[coordinates.count-1]) < (-100){
            print("Vers la droite et bas")
            direction1[index] = "downright"
        }
        else if (coordinates[0] - coordinates[coordinates.count-2]) > 100 &&  (coordinates[1] - coordinates[coordinates.count-1]) > 100 {
            print("Vers la gauche et haut")
            direction1[index] = "upleft"
        }
        else if (-100..<100).contains(coordinates[0] - coordinates[coordinates.count-2]) && (coordinates[1] - coordinates[coordinates.count-1]) > 100 {
            print("Swipe up")
            direction1[index] = "swipeup"
        }
        else if (-100..<100).contains(coordinates[0] - coordinates[coordinates.count-2]) && (coordinates[1] - coordinates[coordinates.count-1]) < -100 {
            print("Swipe Down")
            direction1[index] = "swipedown"
        }
        else if (coordinates[0] - coordinates[coordinates.count-2]) > 100 && (-100..<100).contains(coordinates[1] - coordinates[coordinates.count-1]){
            print("Swipe left")
            direction1[index] = "swipeleft"
        }
        else if (coordinates[0] - coordinates[coordinates.count-2]) < -100 && (-100..<100).contains(coordinates[1] - coordinates[coordinates.count-1]){
            print("Swipe right")
            direction1[index] = "swiperight"
        }
        else {
            direction1[index] = "failed"
        }
        return direction1[index]
    }
}

【问题讨论】:

  • 使用了很多数组。请调试并检查索引超出范围的位置。
  • @DSDharma 你是对的,我的错。那是我在以下行调用函数 GestureRecognizer 时:direction1[index] = "Something" 这就是我得到错误的地方。

标签: ios arrays swift indexing swift3


【解决方案1】:

您应该重新检查您的 direction1 数组。错误很可能在这一行:

print("1 " + direction1[0] + "2 " + direction1[1] + "3 " + direction1[2] + "4 " + direction1[3] + "5 " + direction1[4])

您已经设置了direction1 = [],然后您尝试在direction1 数组中打印5 个不同的元素。

解决方法是初始化一个固定大小的数组[String](repeating: "", count: 5),并去掉这行direction1 = []

【讨论】:

  • 实际上错误在direction1[index] = "Something",就是它出现的地方。如何分别获取每个点的方向?
  • 是的,如果调用了GestureRecognizer,它就在direction1[index] = "Something" 中。否则,它位于touchesEnded。检查我修改后的答案。
  • @Hawkydoky 不用担心。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 2018-05-06
  • 2016-08-15
相关资源
最近更新 更多