EXC_BAD_INSTRUCTION(代码= EXC_1386_INBOP,子代码= 0x0

时间:2017-03-09 23:25:15

标签: swift

我正在尝试从SQL表中检索数据。

在第一段代码中,我可以使用正确的输出

执行此操作
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.
}

在第二个代码段中,我遇到EXC_BAD_INSTRUCTION(code=EXC_1386_INBOP, subcode=0x0行上的错误print("Username is: ", itemName!),即使它们是相同的代码。

谷歌搜索这个问题让我相信&#34; itemName&#34;是零,因此让我觉得我没有正确阅读SQL。

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.
}

2 个答案:

答案 0 :(得分:1)

如果userName!userName,我认为nil会崩溃。您应该在nil之前检查print

答案 1 :(得分:0)

感谢您的回复,我改变了界限:

“让itemName =(stocks [IndexPath.Element.init(i)]为?[String:String])?[”itemName“]”

为:

“让itemName =(stocks [IndexPath.Element.init(i)]为?[String:Any])?[”itemName“]”

似乎已经完成了这项工作