SwiftUI :Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
In the context of Swift code,
EXC_BAD_INSTRUCTION
usually means you’ve hit a compiler trap, that is, an undefined instruction inserted into the code by the compiler because of a bug detected at runtime. The most common cause of these are:
failure to unwrap an optional —
- This can be a forced unwrap (!) or an implicit unwrap (accessing an implicitly unwrapped optional that’s nil).
- array out of bounds
- a failed forced cast (as!), either because the value was a nil optional or because the value was of the wrong type
You can debug this issue by creating a exception breakpoint. As the name suggests, this stops the code execution before the execution of the line that throwed this exception.
To Create a exception breakpoint in Xcode, Go to BreakPoint navigator -> Click the + icon at the bottom left corner -> Select Exception Breakpoint.
More about breakpoints check this link
EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) - underlying causes
The identifier "EXC_BAD_INSTRUCTION"
seems to indicate an attempt to execute an invalid opcode. On IA32 hardware, it is a fault of type Invalid Opcode Exception (#UD) and has interrupt vector value of 6. It can be triggered for a number of reasons but it generally means that the byte sequence that encode the incoming instruction is invalid or reserved, or has inconsistent operands relative to the instruction, or is to long (>15 bytes), or uses an IA32 extension not supported by the current hardware, or is the UD2 opcode, or attempting to execute some instruction while the machine is in some state that prevents its execution, or some other corner cases.
Now, one possible explanation for cause for this kind of fault is that the compiler you are using assumes that some hardware features are accessible to the target (executing) machine and compiles code accordingly. The target machine features can generally specified as compile flag options. For example, floating point operations and standard math functions will normally only generate x87 fpu instruction, but using the combination of -mfpmath=sse and -msse instruct compiler to generate SSE scalar instructions for usual floating point calculations. The SEE instruction sets are an extension feature of IA32 architecture and are not available on all machines. Portable code for an architecture should be compiled to machine independent generic code for this architecture.
Another possible but less probable cause for this hardware fault is that the compiler might itself be bugged and generate some invalid byte sequence.
I don't think some undefined behavior would lead to an invalid opcode fault on most architectures. While it is true that UB may lead to any kind of erratic behavior, UB conditions are mostly never detectable at compile time and as such, the usually generate a General Protection Fault (#GP) on IA32 (which is called a segmentation fault in unix nomenclature). On the opposite, attempting to generate an undefined opcode is a condition that is always detectable at compile time (unless the code is self generating at runtime, or if the byte code stream gets misaligned and the instruction pointer points in the middle of some opcode).
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) func prepare
The problem is that the outlets are not connected (yet) in the destination view controller during prepare(for segue
.
You need a temporary property in ProductViewController
and configure the outlets in viewDidLoad
var product : Product! // replace Product with the real type
func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.main.async {
let brand = product.brand["name"] as? String ?? ""
self.productBrandLabel.text = brand.uppercased()
self.productNameLabel.text = product.name
self.productPriceLabel.text = product.price.stringValue
}
}
and replace prepare(for segue
with
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case toProductIdentifier:
let destination = segue.destination as! ProductViewController
let cell = sender as! ProductCollectionViewCell
let indexPath = collectionView.indexPath(for: cell)!
destination.product = products[indexPath.item]
default: break
}
}
Edit:
Obviously you connected the cell to the segue rather than the controller. In this case the cell
is passed in the sender
parameter and didSelectItemAt
is not being called. Delete selectedRowIndex
and delete also the entire didSelectItemAt
method
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let product = products[indexPath.row]
print(product.name)
collectionView.deselectItem(at: indexPath, animated: true)
self.performSegue(withIdentifier: toProductIdentifier, sender: product)
}
what does Error “Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)” mean?
It means that there there are instructions that lead to a crash, such as force unwrapping something that doesn't exist, and getting a value of nil.
Take a look through your code and see if there are any situations where you force unwrap something that does not necessarily exist.
sReto iOS Swift 5.0
Not sure you found an answer yet, but I had the same problem and googled a bit. Turns out the hashable calculation is deprecated and automated now, so removing the whole
public var hashValue: Int { // CAUTION HERE
return uuid.map { $0.hashValue }.enumerated().reduce(0,
{
let (index, hash) = $1
return $0 ^ (hash << index * 2) // CRASH HERE
}
)
}
fixed the problem and resulted in working code!
Related Topics
Uinavigationcontroller "Back Button" Custom Text
How to Specify Size for iPhone 6/7 Customised Edge-To-Edge Image
iOS Static VS Dynamic Frameworks Clarifications
Steps to Upload an iPhone Application to the Appstore
Instead of Push Segue How to Replace View Controller (Or Remove from Navigation Stack)
How to Open Mail App from Swift
Xcode 8 Build Crash on iOS 9.2 and Below
How to Keep Uitableview Contentoffset After Calling -Reloaddata
Force Landscape Mode in One Viewcontroller Using Swift
How to Programmatically Fake a Touch Event to a Uibutton
Missing Private Key in the Distribution Certificate on Keychain
Remove Println() for Release Version iOS Swift
Creating a Reusable Uiview with Xib (And Loading from Storyboard)
Animate Uilabel Text Between Two Numbers
Tutorial on How to Drag and Drop Item from Uitableview to Uitableview