我正在尝试处理Swift中的一个应用,并且遇到使用数组的以下错误:
fatal error: Array index out of range
当应用为索引0的数组分配值时,将显示以下内容:
class DrawScene: SKScene { init(size: CGSize) { super.init(size: size) } var line = SKShapeNode() var path = CGPathCreateMutable() var touch: UITouch! var pts = [CGPoint]() override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { /* Called when a touch begins */ touch = touches.anyObject() as UITouch! self.pts[0] = touch.locationInNode(self) <-- Error appears here drawLine() }
有什么想法吗?(我正在使用xcode 6 Beta 4)
您的数组最初是空的。
if self.pts.isEmpty { self.pts.append(touch.locationInNode(self)) } else { self.pts[0] = touch.locationInNode(self) }