in a search field error Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

448 views Asked by At

in a search field if I write in it gives me this error:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

If I type a comma, or a number (so the result is not there) the app works, but if I enter a letter (then find a result) the app crashes.

enter image description here

override func updateControllerViews(_ animated: Bool) {
        super.updateControllerViews(animated)
        
        if textFieldSearch.text == "" {
            containerButtons.isHidden = false
            containerCollectionView.isHidden = true
            imageDeletSearch.image = NSImage.init(named: "menu_header_cerca")
            buttonDeleteSearch.isEnabled = false
        } else {
            containerButtons.isHidden = true
            containerCollectionView.isHidden = false
            imageDeletSearch.image = NSImage.init(named: "searchoff")
            buttonDeleteSearch.isEnabled = true
            
            updateDataSource()
        }
        
        collectionView.reloadData()
    }
func updateDataSource() {
        dataSource = DeviceSearchKeywords.filteredKeywords(byString: textFieldSearch.text)
    }
extension DeviceSearchKeywords {
    static func filteredKeywords(byString string: String?) -> [String : DeviceType] {
        guard let string = string else {
            return [:]
        }
        
        let cleanString = StringUtility.cleaning(string).lowercased()
        
        var filteredKeyword: [String : DeviceType] = [:]
        
        for keyword in mainDeviceKeywords {
            if keyword.lowercased().contains(cleanString) {
                filteredKeyword[keyword] = .deviceMain
            }
        }
    
        for keyword in importDeviceKeywords {
            if keyword.lowercased().contains(cleanString) {
                filteredKeyword[keyword] = .deviceImport
            }
        }
        
        for keyword in exportDeviceKeywords {
            if keyword.lowercased().contains(cleanString) {
                filteredKeyword[keyword] = .deviceExport
            }
        }

        
        return filteredKeyword
    }
}
1

There are 1 answers

12
Alastar On

EXC_BAD_INSTRUCTION means that you have an undefined instruction inserted into the code and it's detected at runtime.

You can have either a:

  • Failing a force cast (as!) but there's no evidence in the code you posted
  • Array out of bounds
  • trying to force unwrap (!) a nil optional property

Try do debug your code activating Exception breakpoints if you haven't done it already!