2017-03-09 52 views
0

我試圖從SQL表中檢索數據。EXC_BAD_INSTRUCTION(代碼= EXC_1386_INBOP,子碼=爲0x0

在代碼的第一個片段,我能夠用正確的輸出

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

做到這一點在第二個片段中,我遇到了線print("Username is: ", itemName!)上的錯誤EXC_BAD_INSTRUCTION(code=EXC_1386_INBOP, subcode=0x0,即使它們是相同的代碼。

使用Google搜索導致我相信「itemName」是零,因此使我認爲我不讀在prop中的SQL erly。

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

如果「他們是相同的代碼」發生了什麼變化在你列出代碼兩次? –

+0

我列出了兩次,直到任何人都可以看到我可能犯的錯誤 – GCSmith

回答

1

我想,如果userNameniluserName!會崩潰。您應該在print之前檢查nil

0

謝謝你的答覆,我改變了行:

「讓ITEMNAME =(股票[IndexPath.Element.init(I)]爲[字符串:字符串])?[」 ITEMNAME 「]」

到:

「讓ITEMNAME =(股票[IndexPath.Element.init(I)]爲[字符串:任何])?[」 ITEMNAME 「]」

和似乎已做工作

相關問題