Swift Addition: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (Code=EXC_l386_INVOP, subcode=0x0)

我只是一个虾纸丫 提交于 2019-12-12 03:34:09

问题


I'm trying to add two numbers in Swift and print its sum

import Foundation

func solveMefirst(firstNo: Int , secondNo: Int) -> Int {
    return firstNo + secondNo
}

let num1 = readLine()
let num2 = readLine()

var IntNum1 = Int(num1!) ** Execution was interrupted, reason: EXC_BAD_INSTRUCTION (Code=EXC_l386_INVOP, subcode=0x0). **
var IntNum2 = Int(num2!)

let sum = solveMefirst(IntNum1!, secondNo: IntNum2!)
print(sum)

But unfortunately this error comes out and stops the execution in playground.

Execution was interrupted, reason: EXC_BAD_INSTRUCTION (Code=EXC_l386_INVOP, subcode=0x0).

Could not understand, what is wrong in this?

UPDATE

Also please explain how to run this command line program in playground?

How can I take input from playground??.


回答1:


It crashes because you force a nil value which results in an error; As you can read on the left num1= nil. It don't know what it is supposed to be but there is the error. Because when you try to force a value to num1(that's what "!" does) it crashes because num1 has no value.




回答2:


I ran it using command line. I think you are using play ground but you didn't take input for num1 and num2. After running the project take input for number 1 then press enter. Then take input for number two and press enter. It will give you the desired result. Your code is working fine. I run it here.



来源:https://stackoverflow.com/questions/35347755/swift-addition-execution-was-interrupted-reason-exc-bad-instruction-code-exc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!