0

I am making a Search system now. If the text is entered in the text field and its value is in the database, it transitions to the next screen and displays the acquired data. On the debug screen, since the acquired value is displayed, I think the API is working.However, I get an error with EXC_BAD_INSTRUCTION (code = EXC_I386_INVOP, subcode = 0x0) I am stuck for 5 hours with this error. please tell me. Thank you.

enter image description here

SearchViewController

import UIKit
import Alamofire
class SearchViewController: UIViewController {
//API
let SEARCH_CODE = "API URL"
//devaultUserValue
let defaultValues = UserDefaults.standard



@IBOutlet weak var searchCodeFeild: UITextField!
@IBOutlet weak var labelMessage: UILabel!


@IBAction func searchBtn(_ sender: UIButton) {

    //Getting device code 
    let parameters: Parameters=[
    "deviceCode" : searchCodeFeild.text!
    ]

    //making post request

    Alamofire.request(SEARCH_CODE, method: .post, parameters: parameters).responseJSON
        {
            response in
            //printing response
            print(response)

            //getting the json value from the server

            if let result = response.result.value{

                let jsonData = result as! NSDictionary

                //if there is no error
                if(!(jsonData.value(forKey: "error")as! Bool)){
                    let device = jsonData.value(forKey: "device") as! NSDictionary

                    //getting user value
                    let deviceID = device.value(forKey: "id") as! Int
                    let deviceName = device.value(forKey: "name") as! String
                    let deviceCode = device.value(forKey: "deviceCode") as! String


                    self.defaultValues.set(deviceID, forKey: "id")
                    self.defaultValues.set(deviceName, forKey: "name")
                    self.defaultValues.set(deviceCode, forKey: "deviceCode")


                    let resultViewController = self.storyboard?.instantiateViewController(withIdentifier: "resultViewController") as! ResultViewController
                    self.navigationController?.pushViewController(resultViewController, animated: true)

                    self.dismiss(animated: false, completion: nil)

                }else{
                    self.labelMessage.text = "Sorry not find device"
                }
            }
    }
}




override func viewDidLoad() {
    super.viewDidLoad()



}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

import UIKit class ResultViewController: UIViewController {

@IBOutlet weak var deviceNameFeild: UILabel!


@IBAction func backBtn(_ sender: UIButton) {

    //userDefaultにあるデーターを消す
    UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
    UserDefaults.standard.synchronize()

    let searchViewController = self.storyboard?.instantiateViewController(withIdentifier: "SearchViewController") as! SearchViewController
    self.navigationController?.pushViewController(searchViewController, animated: true)
    self.dismiss(animated: false, completion: nil)
}


override func viewDidLoad() {
    super.viewDidLoad()

    let backBtn = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.plain, target: navigationController, action: nil)
    navigationItem.leftBarButtonItem = backBtn

    let defaultValues = UserDefaults.standard
    if let name = defaultValues.string(forKey: "name"){
        //setting the name to label
        deviceNameFeild.text = name
    }else{
        //send back to login view controller
    }


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Alex
  • 71
  • 2
  • 9
  • Check if your `IBOutlets` are wired up properly. Since you force unbox everything you are bound to get runtime errors. – Wukerplank Sep 11 '17 at 07:45
  • I suspect that your json object 'device' is not convertible in Dictionary, you are forcing to convert it in Dictionary. Post the json. – Shalva Avanashvili Sep 11 '17 at 07:56
  • I'm checking now. IBOutlets are are wired up properly. – Alex Sep 11 '17 at 07:57
  • Please post the exact exception text. INVOP, subcode = 0x0 typically occurs when unwrapping an optional that is nil. – Andreas Oetjen Sep 11 '17 at 08:05
  • 1
    Do not use `NSDictionary` / `NSArray` in Swift. Do not use `valueForKey:` unless you can explain why you explicitly need KVC. – vadian Sep 11 '17 at 08:23

0 Answers0