我正在尝试处理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) }