为什么我收到错误EXC_BAD_INSTRUCTION(code = EXC_I386_INVOP,subcode = 0x0)

时间:2016-01-25 13:04:09

标签: ios swift

为什么我会收到错误

  

EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)

in

let SpawnPoint = UInt32(MaxValue - MinValue)

在以下代码中:

func SpawnRain() {
    Rain = SKSpriteNode(imageNamed: "Rain")
    let MinValue = self.size.width / 8
    let MaxValue = self.size.width - 40
    let SpawnPoint = UInt32(MaxValue - MinValue)
    Rain.size = CGSize(width: 32, height: 32)
    Rain.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
    Rain.physicsBody = SKPhysicsBody(rectangleOfSize: Rain.size)
    Rain.physicsBody?.categoryBitMask = PhysicsCatagory.Rain
    Rain.physicsBody?.contactTestBitMask = PhysicsCatagory.Player
    Rain.physicsBody?.affectedByGravity = false
    Rain.physicsBody?.dynamic = true

let action = SKAction.moveToY(-70, duration: 8.0)
    let actionDone = SKAction.removeFromParent()
    Rain.runAction(SKAction.sequence([action, actionDone]))
    self.addChild(Rain)
}

1 个答案:

答案 0 :(得分:0)

如果宽度很小,那么MaxSize - MinSize< 0,出于安全考虑,尝试将负数转换为UInt32将会崩溃。这是Swift设计的一部分。在一种不太安全的语言中,arc4random_uniform会返回一个巨大的值,只会导致麻烦并导致难以发现的错误。