2017-06-05 19 views
1

我在Xcode 8中使用swift 3製作了一個ios遊戲應用程序,並且一直收到一個錯誤,指出「線程1:EXC_BAD_INSTRUCTION(codeEXC_1386_INVOP ,subcode = 0x0)「和一個控制檯消息,表示致命錯誤:」索引超出範圍(lldb)「。有誰知道如何解決這一問題?這個錯誤不斷出現在我的代碼中,我無法弄清楚如何修復它

下面是帶有錯誤的代碼部分。我得到它,說行了「讓節點B = 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) 
    } 

回答

2

的「超出範圍」的錯誤是許多編程語言中常見的。它向你表明循環試圖訪問數組中超出數組範圍的位置。

根據上面的代碼,不可能找出你在哪裏獲得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) 
} 
+0

THX!這解決了它! – Shock9616

相關問題