xcode – 整数溢出在Swift中给出了EXC_BAD_INSTRUCTION

前端之家收集整理的这篇文章主要介绍了xcode – 整数溢出在Swift中给出了EXC_BAD_INSTRUCTION前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在搞乱 Swift时,我注意到当64位整数溢出时,我收到以下错误

file:///Users/user/Documents/playground/MyPlayground.playground/: error: Playground execution aborted: Execution was interrupted,reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0).

  1. func fibonacci(which: Int) -> (fibOf: Int,isEqualTo: Int) {
  2. var i = 1,j = 1
  3.  
  4. for var k = 2; k < which; k += 1 {
  5. let tmp = i + j // this line is highlighted when error occurs
  6. j = i
  7. i = tmp
  8. }
  9.  
  10. return (which,i)
  11. }
  12.  
  13. print (fibonacci(92))
  14. print (fibonacci(93)) // this triggers an error

第一个调用,即92作为参数,将运行良好.但是,当提供93值时,我得到了无关的EXC_BAD_INSTRUCTION错误.这是一个错误还是什么?通常我会期望它溢出.

解决方法

这是预期的行为.如果要溢出,则需要使用 overflow operators.

>溢出添加(&)>溢出减法(&amp

猜你在找的iOS相关文章