GCSmith
GCSmith

Reputation: 1

EXC_BAD_INSTRUCTION(code=EXC_1386_INBOP, subcode=0x0

I am trying to retrieve data from an SQL table.

In the first snippet of code I am able to do this with the correct output

private func loadAllEmployees(){
    //URL:
    let URL_GET_EMPLOYEES:String = "URL_HERE"

    //created NSURL
    let requestURL = NSURL(string: URL_GET_EMPLOYEES)

    //creating NSMutableURLRequest
    let request = NSMutableURLRequest(url: requestURL! as URL)

    //setting the method to post
    request.httpMethod = "GET"

    //creating a task to send the post request
    let task = URLSession.shared.dataTask(with: request as URLRequest){
        data, response, error in

        //exiting if there is some error
        if error != nil{
            print("error is \(error)")
            return;
        }

        //parsing the response
        do {
            //converting resonse to NSDictionary
            var employeeJSON: NSDictionary!
            employeeJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

            //getting the JSON array teams from the response
            let employees: NSArray = employeeJSON["employees"] as! NSArray

            //looping through all the json objects in the array teams
            let endOfArray = employees.count

            for i in 0 ..< endOfArray{
                //getting the data at each index
                let userName = (employees[IndexPath.Element.init(i)] as? [String : String])? ["userName"]

                //displaying the data
                print("Username is: ", userName!)
            }
        } catch {
            print(error)
        }
    }

    //executing the task
    task.resume()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    loadAllEmployees()
}

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

Within the second snippet I come across the error EXC_BAD_INSTRUCTION(code=EXC_1386_INBOP, subcode=0x0 on the line print("Username is: ", itemName!) even though they are identical code.

Googling this problem has led me to believe that "itemName" is nil, thus making me think that I am not reading in the SQL in properly.

private func loadAllStock(){
    //URL:
    let URL_GET_STOCK:String = "URL_HERE"

        //created NSURL
        let requestURL = NSURL(string: URL_GET_STOCK)

        //creating NSMutableURLRequest
        let request = NSMutableURLRequest(url: requestURL! as URL)

        //setting the method to post
        request.httpMethod = "GET"

        //creating a task to send the post request
        let task = URLSession.shared.dataTask(with: request as URLRequest){
            data, response, error in

            //exiting if there is some error
            if error != nil{
                print("error is \(error)")
                return;
            }

            //parsing the response
            do {
                //converting resonse to NSDictionary
                var stockJSON: NSDictionary!
                stockJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                //getting the JSON array teams from the response
                let stocks: NSArray = stockJSON["stocks"] as! NSArray

                //looping through all the json objects in the array teams
                let endOfArray = stocks.count

                for i in 0 ..< endOfArray{
                    //getting the data at each index
                    let itemName = (stocks[IndexPath.Element.init(i)] as? [String : String])? ["itemName"]

                    //displaying the data
                    print("ItemName is: ", itemName!)
                    print("===================")
                    print("")
                }
            } catch {
                print(error)
            }
        }

        //executing the task
        task.resume()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    loadAllStock()
}

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

Upvotes: 0

Views: 49

Answers (2)

GCSmith
GCSmith

Reputation: 1

Thanks for the replies, I changed the line:

"let itemName = (stocks[IndexPath.Element.init(i)] as? [String : String])? ["itemName"]"

to:

"let itemName = (stocks[IndexPath.Element.init(i)] as? [String : Any])? ["itemName"]"

and has seemed to do the job

Upvotes: 0

Dominic Dos Santos
Dominic Dos Santos

Reputation: 2713

I think userName! will crash if userName is nil. You should check for nil before the print.

Upvotes: 1

Related Questions