ios 错误“线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28804654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what does Error "Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" mean?
提问by legolas
I got this error:
我收到此错误:
Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
线程 1:EXC_BAD_INSTRUCTION(代码=EXC_I386_INVOP,子代码=0x0)
How can I solve this? The code works normally, but in the calculator when I click the only equal button, it gives that error.
我该如何解决这个问题?该代码正常工作,但在计算器中,当我单击唯一的相等按钮时,它给出了该错误。
@IBAction func equals(sender: AnyObject) {
secondNumber = Screen.text!.toInt()! // here it shows an error which is "Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"
if operation == "+"{
result = firstNumber + secondNumber
}
else if operation == "-" {
result = firstNumber - secondNumber
}
else if operation == "x" {
result = firstNumber * secondNumber
}
else {
result = firstNumber / secondNumber
}
Screen.text = "\(result)"
}
回答by gnasher729
This line
这条线
secondNumber = Screen.text!.toInt()!
means: Get the Screen object, get the text property and please crash if it doesn't exist, then get the text converted to an integer, and please crash if it doesn't exist.
意思是:获取Screen对象,获取text属性,如果不存在请崩溃,然后将文本转换为整数,如果不存在请崩溃。
That's what the ! means: It means "I am sure this thing exists, so please crash if it doesn't". And that's what it did.
就是这样!意思是:意思是“我确定这个东西存在,所以如果它不存在,请崩溃”。这就是它所做的。
回答by Ashraf Tawfeeq
Generally, EXC_BAD_INSTRUCTION means that there was an assertion failure in your code. A wild guess, your Screen.text
is not an integer. Double check its type.
通常, EXC_BAD_INSTRUCTION 表示您的代码中存在断言失败。一个疯狂的猜测,你Screen.text
不是一个整数。仔细检查它的类型。
回答by alicanbatur
Mine was about
我的是关于
dispatch_group_leave(group)
dispatch_group_leave(组)
was inside if closure in block. I just moved it out of closure.
如果在块中关闭,则在里面。我刚把它从关闭中移出。
回答by J. Doe
I got this error while I tried to write to a variable at the same time from different threads. Creating a private queue and making sure one thread at a time can write to that variabele at the same time. It was a dictionary in my case.
当我尝试从不同线程同时写入变量时出现此错误。创建一个私有队列并确保一次一个线程可以同时写入该变量。就我而言,它是一本字典。
回答by pankaj nigam
mine was DispatchQueue.main.sync inside the closer I made it DispatchQueue.main.async and it worked.
我的是 DispatchQueue.main.sync 里面更接近我使它 DispatchQueue.main.async 并且它工作。
回答by Teo
In my case it happened when calling a function by passing a parameter of a Core Data managed object's property. At the time of calling the object was no longer existed, and that caused this error.
在我的情况下,它发生在通过传递 Core Data 托管对象属性的参数来调用函数时。在调用对象时不再存在,这导致了这个错误。
I have solved the issue by checking if the managed object exists or not before calling the function.
我已经通过在调用函数之前检查托管对象是否存在来解决这个问题。
回答by Kiwi
In my case this was caused by an integer overflow. I had a UInt16, and was doubling the value to put into an Int. The faulty code was
在我的情况下,这是由整数溢出引起的。我有一个 UInt16,并将值加倍以放入 Int。错误的代码是
let result = Int(myUInt16 * 2)
However, this multiplies as a UInt16, then converts to Int. So if myUInt16 contains a value over 32767 then an overflow occurs.
但是,这乘以 UInt16,然后转换为 Int。因此,如果 myUInt16 包含超过 32767 的值,则会发生溢出。
All was well once I corrected the code to
一旦我将代码更正为
let result = Int(myUint16) * 2
回答by Helge Becker
Your secondNumber seems to be an ivar, so you have to use a local var to unwrap the optional. And careful. You don't test secondNumber for 0, which can lead into a division by zero. Technically you need another case to handle an impossible operation. For instance checkin if the number is 0 and do nothing in that case would at least not crash.
您的 secondNumber 似乎是一个 ivar,因此您必须使用本地 var 来解开可选项。并且小心。您不测试 secondNumber 是否为 0,这可能导致除以零。从技术上讲,您需要另一个案例来处理不可能的操作。例如,检查数字是否为 0 并且在这种情况下什么都不做至少不会崩溃。
@IBAction func equals(sender: AnyObject) {
guard let number = Screen.text?.toInt(), number > 0 else {
return
}
secondNumber = number
if operation == "+"{
result = firstNumber + secondNumber
}
else if operation == "-" {
result = firstNumber - secondNumber
}
else if operation == "x" {
result = firstNumber * secondNumber
}
else {
result = firstNumber / secondNumber
}
Screen.text = "\(result)"
}
回答by Szilveszter Zsigmond
If someone else have got a similar error message, you probably built the app with optimisations(production) flag on and that's why the error message is not that communicative. Another possibility that you have got the error under development (from i386 I know you were using simulator). If that is the case, change the build environment to development and try to reproduce the situation to get a more specific error message.
如果其他人收到了类似的错误消息,则您可能在构建应用程序时启用了优化(生产)标志,这就是错误消息不那么具有交流性的原因。另一种可能是您在开发中遇到错误(从 i386 我知道您正在使用模拟器)。如果是这种情况,请将构建环境更改为 development 并尝试重现该情况以获得更具体的错误消息。