我在尝试使用 Swift 3 在 Xcode 中显示 JSON 数据时遇到构建错误.我将把我的部分代码复制到此页面,希望你们能帮助我.
I am getting build errors when trying to display jSON data in Xcode using Swift 3. I am going to copy a portion of my code to this page with hopes you guys can assist me.
我在这个网站上发现了类似的问题,但答案似乎没有帮助.
I have found similar questions on this site however answers don't seem to help.
class FilmsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { weak var tableView : UITableView! var FilmArray = [String]() let film_url = "www.testing/api/resources/films/1" func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for:indexPath) as! FilmsAPITableViewCell // Adding the right informations cell.movieTitle.text = FilmArray[indexPath.row] // Returning the cell return cell } // @IBOutlet weak var FilmsView: UITableView! // weak var tableView : UITableView! // var FilmArray = [String]() // // let film_url = "www.distribber/api/resources/films/1" // override func viewDidLoad() { super.viewDidLoad() let tableView = UITableView (frame:view.bounds) view.addSubview(tableView) self.tableView = tableView tableView.dataSource = self tableView.delegate = self // func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // return 1 // } // func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { // // Getting the right element // //let films = FilmArray[indexPath.row] // // // // Instantiate a cell // //let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "moviecell") // let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FilmsAPITableViewCell // // cell.movieTitle.text = FilmArray[indexPath.row] // // Adding the right informations // cell.movieTitle.text = FilmArray[indexPath.row] // // Returning the cell // return cell // } // } //} let url:URL = URL(string: film_url)! let session = URLSession.shared let request = NSMutableURLRequest(url: url) request.httpMethod = "GET" request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY") request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization") request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData let paramString = "" // for (key, value) in post_data // { // paramString = paramString + (key as! String) + "=" + (value as! String) + "&" // } // request.httpBody = paramString.data(using: String.Encoding.utf8) let task = session.dataTask(with: request as URLRequest, completionHandler: { ( data, response, error) in guard let _:Data = data, let _:URLResponse = response , error == nil else { return } let json: Any? do { json = try JSONSerialization.jsonObject(with: data!, options: []) // Prasing JSON var parsedData = try JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any] print(parsedData) if let FilmArray = parsedData["films"] as? NSArray { for movieTitle in FilmArray{ if let filmDict = movieTitle as? NSDictionary{ if let film = filmDict.value(forKey: "title") { self.FilmArray.append(film as! String) } OperationQueue.main.addOperation({ self.tableView.reloadData() }) } } } print("Hello") self.tableView.reloadData() print(self.FilmArray) } catch { return } guard let server_response = json as? NSDictionary else { return } if let data_block = server_response["data"] as? NSDictionary { if let session_data = data_block["session"] as? String { // self.login_session = session_data let preferences = UserDefaults.standard preferences.set(session_data, forKey: "session") // DispatchQueue.main.async(execute: self.LoginDone) } } }) task.resume() // Do any additional setup after loading the view. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }这里也是 FilmsAPITableViewCell.swift 的输出
Here is also output from FilmsAPITableViewCell.swift
import UIKit导入 UIKit
class FilmsAPITableViewCell: UITableViewCell { @IBOutlet weak var movieTitle: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } } 推荐答案movieTitle 为 nil 的原因是因为您的自定义单元格类没有那个标签出口.您需要在您的 .xib(或故事板)中创建一个 UILabel 并在您的自定义单元格类中创建一个插座连接.
The reason why movieTitle is nil is because your custom cell class does not have that label outlet. You need to create a UILabel in your .xib (or storyboard) and create an outlet connection inside your custom cell class.
您的完成块中似乎也不存在 self.tableView.reloadData() .尝试在 print("Hello") 行之后添加它.
It also seems like no self.tableView.reloadData() exists in your completion block. Try adding that right after the print("Hello") line.
附言不要忘记将重新加载分派到主队列.
P.S. Don't forget to dispatch the reload to the main queue.
这也是我编辑的代码以使其正常工作:
Here is also your code which I edited as to get it working:
override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self tableView.delegate = self let url:URL = URL(string: film_url)! let session = URLSession.shared let request = NSMutableURLRequest(url: url) request.httpMethod = "GET" request.setValue("740c94c51891c02b64d6c78840b478fe0b02fe2c", forHTTPHeaderField: "X-API-KEY") request.setValue("Basic YmhlZW0uZW5nckBnbWFpbC5jb206YmgzM20=", forHTTPHeaderField: "Authorization") request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData let paramString = "" request.httpBody = paramString.data(using: String.Encoding.utf8) let task = session.dataTask(with: request as URLRequest, completionHandler: { ( data, response, error) in guard let _:Data = data, let _:URLResponse = response , error == nil else { return } var json:Any? do { if let existingData = data { json = try JSONSerialization.jsonObject(with: existingData, options: []) } // Prasing JSON if let parsedData = json as? [[String:Any]] { for dict in parsedData { if let title = dict["title"] as? String { self.FilmArray.append(title) } } OperationQueue.main.addOperation({ self.tableView.reloadData() }) } } catch { return } guard let server_response = json as? NSDictionary else { return } if let data_block = server_response["data"] as? NSDictionary { if let session_data = data_block["session"] as? String { // self.login_session = session_data let preferences = UserDefaults.standard preferences.set(session_data, forKey: "session") // DispatchQueue.main.async(execute: self.LoginDone) } } }) task.resume() }更多推荐
UITable 不会显示.值类型“FilmsAPITableViewCell"没有成员“movieTitle"
发布评论