私は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.
}
をこれを行うことができますよ2番目のスニペットでは、同じコードであっても、エラーEXC_BAD_INSTRUCTION(code=EXC_1386_INBOP, subcode=0x0
の行print("Username is: ", itemName!)
に出くわします。
この問題の原因は、「itemName」がnilであると考えているためです。 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.
}
「彼らは同じコードです」と変更されたものあなたはコードを二度列挙しますか? –
誰かが私が作ったかもしれないエラーを見ることができたら – GCSmith