我的swift程序出现了这个错误,我使用PHP和MySQL作为数据库。我将数据从数据库显示到tableview。它以前工作但经过几次运行到模拟器后,我一直有同样的错误
class Home: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var btnaddclass: UIButton!
@IBOutlet var tableView: UITableView!
var values: NSArray = []
func get(){
let url = NSURL(string: "http://localhost/show_db.php")
let data = NSData(contentsOf: url! as URL)
values = try! JSONSerialization.jsonObject(with: data! as Data, options:JSONSerialization.ReadingOptions.mutableContainers)as! NSArray
tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return values.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! SpecialCell
let maindata = values[indexPath.row] as! [String:AnyObject]
cell.Lblclasstitle.text = maindata["class_title"] as? String
cell.Lblschool.text = maindata["school"] as? String
cell.Lblsubject.text = maindata["subject"] as? String
return cell
}
override func viewDidLoad() {
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
get()
}
override func viewWillAppear(_ animated: Bool) {
self.tableView.reloadData()
}
}
答案 0 :(得分:7)
正如其他一些答案所指出的那样,问题在于您正在强制进行JSON反序列化,并假设它始终在100%的时间内正常工作。这几乎总是不太好的选择。有两种方法可以解决这个问题:
如果您尝试执行的语句失败,那么这将是一个很好的选择,然后您将获得nil然后可以基于此做一些事情。例如,您可以将get()函数更改为:
func get(){
//remove NS prefix and unwrap optional
guard let url = URL(string: "http://localhost/show_db.php") else { return }
//remove ! from url and unwrap optional
guard let data = try? Data(contentsOf: url) else { return }
//try to deserialize. The return value is of type Any
guard let deserializedValues = try? JSONSerialization.jsonObject(with: data) else { return }
//Convert to array of dictionaries
guard let arrayOfDictionaryValues = deserializedValues as? [[String:String]] else { return }
//If you make it here, that means everything is ok
values = arrayOfDictionaryValues
tableView.reloadData()
}
请注意,每个保护语句都为您提供了执行某些操作并返回的机会。也许您想显示一条消息,指出存在网络错误。
我认为使用试试?更适合你的情况但是在某些情况下尝试catch块是合适的。例如:
func getWithErrorHandling() {
guard let url = URL(string: "http://localhost/show_db.php") else { return }
do {
let data = try Data(contentsOf: url)
let deserializedValues = try JSONSerialization.jsonObject(with: data)
guard let arrayOfDictionaryValues = deserializedValues as? [[String:String]] else { return }
values = arrayOfDictionaryValues
} catch {
//You should separate the different catch blocks to handle the different types of errors that occur
print("There was an error")
}
}
您可以对整体方法进行其他改进,但由于您是初学者(如您所述),最好一次学习一件事。
此外,正如其他人所提到的,您不应该从主线程获取数据。请改用URLSession。这篇文章就是一个很好的例子:
答案 1 :(得分:3)
1.不要在Swift3中使用NS...
类。例如:
let url = URL(string: "http://localhost/show_db.php")
let data = Data(contentsOf: url!)
2.使用do-try-catch进行异常。
do {
try ...
} catch {
// handle exception
}
3.除非你确定它是哪个类,否则不要使用as!
。使用此:
if let array = some as? Array {
// use array now
}
答案 2 :(得分:1)
您的代码充满了问题。
"尝试!"如果您正在调用的代码抛出异常,表达式将崩溃。这就是你崩溃的原因。不要这样做,特别是在处理来自外部源的数据时。请改用do { try.. } catch { }
。在Apple的文档和在线文档中有很多关于swift try / catch块的例子。
您收到的错误消息告诉您错误。您在JSON数据流的末尾获得了垃圾字符。将您的数据流转换为字符串并记录它。
不要使用as!
,除非你肯定该对象可以被强制转换为所需的类型。 (强制转换失败后会崩溃。)
正如@OOPer在评论中所说,你不应该从主线程上的URL加载数据。这会锁定UI直到加载完成,如果加载时间过长,可能会导致应用程序被杀死。您应该使用像URLSession
这样的异步方法。