Swift3 这个错误一直出现在我的代码中,我可以';我想不出怎么修理它
我正在使用swift 3在Xcode 8中制作一个ios游戏应用程序,我不断收到一个错误,上面写着“线程1:EXC_BAD_指令(代码EXC_1386_INVOP,子代码=0x0)”和一条控制台消息,上面写着致命错误:“索引超出范围(lldb)”。有人知道如何解决这个问题吗 下面是代码的错误部分。我在一行上看到它,上面写着“让nodeB=cableSegments[I]”Swift3 这个错误一直出现在我的代码中,我可以';我想不出怎么修理它,swift3,sprite-kit,xcode8,ios10,Swift3,Sprite Kit,Xcode8,Ios10,我正在使用swift 3在Xcode 8中制作一个ios游戏应用程序,我不断收到一个错误,上面写着“线程1:EXC_BAD_指令(代码EXC_1386_INVOP,子代码=0x0)”和一条控制台消息,上面写着致命错误:“索引超出范围(lldb)”。有人知道如何解决这个问题吗 下面是代码的错误部分。我在一行上看到它,上面写着“让nodeB=cableSegments[I]” for i in 1..<length { let nodeA = cableSegments[i -
for i in 1..<length {
let nodeA = cableSegments[i - 1]
let nodeB = cableSegments[i]
let joint = SKPhysicsJointPin.joint(withBodyA: nodeA.physicsBody!, bodyB: nodeB.physicsBody!,
anchor: CGPoint(x: nodeA.frame.midX, y: nodeA.frame.minY))
scene.physicsWorld.add(joint)
}
对于i in 1..在许多编程语言中,“超出范围”错误很常见。它向您指示循环试图访问数组中超出数组范围的位置
根据上面的代码,无法确定从何处获得length
值,但它应该是数组长度
下面的代码可以工作吗
var counter = 0
for i in 0..<cableSegments.count {
counter += 1
if counter == cableSegments.count {
break
}
let nodeA = cableSegments[i]
let nodeB = cableSegments[i + 1]
let joint = SKPhysicsJointPin.joint(withBodyA: nodeA.physicsBody!, bodyB: nodeB.physicsBody!,
anchor: CGPoint(x: nodeA.frame.midX, y: nodeA.frame.minY))
scene.physicsWorld.add(joint)
}
var计数器=0
因为我在0..Thx!这就解决了!