我在视图控制器中使用标签栏,但是当我运行项目然后点击投诉标签时,它会显示错误,如
致命错误:'尝试!'表达式意外地引发了错误:错误Domain = NSCocoaErrorDomain Code = 3840"字符0周围的值无效。" UserInfo = {NSDebugDescription =字符0周围的值无效。}:file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-800.0.63/src/swift/stdlib/public/core/ErrorType.swift,line 178
请告诉我如何解决此错误。
import UIKit
class ComplaintViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate {
@IBOutlet weak var mytableview: UITableView!
////step->1
var ComplaintList:[Complaint]?
var complaint:Complaint?
override func viewDidLoad() {
super.viewDidLoad()
requestForProductList()
mytableview.reloadData()// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()// Dispose of any resources that can be recreated.
}
private func requestForProductList() {
///step->2
var c_url = const_url + ("complaint_view.php")
let url = URL(string:c_url)
var request = URLRequest(url: url!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request) { (result, response, error) in
guard error == nil else {
print(error!.localizedDescription)
return
}
let resultDict = try! JSONSerialization.jsonObject(with: result!, options: .allowFragments)
DispatchQueue.main.async {
self.parseData(result: resultDict as! [String : Any])
}
print(resultDict)
}
task.resume()
}
private func parseData(result:[String:Any]) {
///step->3
let complaintMain = ComplaintMain(fromDictionary: result)
ComplaintList = complaintMain.complaint
mytableview.reloadData()
}
//MARK:- Tableview datasource methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let list = ComplaintList {
return list.count
}
return 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mycell", for:indexPath) as! ComplaintTableViewCell
let complaint = ComplaintList?[indexPath.row]
cell.lbltype.text = complaint?.ctype
let reqUrl = URL(string: const_url + (complaint?.url)!)
let data = NSData(contentsOf: reqUrl!)
if data != nil
{
cell.myimage.image = UIImage(data:data as! Data)
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
complaint = ComplaintList?[indexPath.row]
performSegue(withIdentifier: "select", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc3 = segue.destination as! ComplaintdidselectViewController
vc3.complaint = complaint
}
}