EXC_BAD_INSTRUCTION(代码= EXC_1386_INBOP,子码=为0x0
问题描述:
我试图从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.
}
答
我想,如果userName
是nil
userName!
会崩溃。您应该在print
之前检查nil
。
答
谢谢你的答复,我改变了行:
“让ITEMNAME =(股票[IndexPath.Element.init(I)]为[字符串:字符串])?[” ITEMNAME “]”
到:
“让ITEMNAME =(股票[IndexPath.Element.init(I)]为[字符串:任何])?[” ITEMNAME “]”
和似乎已做工作
如果“他们是相同的代码”发生了什么变化在你列出代码两次? –
我列出了两次,直到任何人都可以看到我可能犯的错误 – GCSmith