代码之家  ›  专栏  ›  技术社区  ›  Yevgen

Swift中转换为字符串的数字出错

  •  0
  • Yevgen  · 技术社区  · 6 年前

    我是Swift和Xcode的初学者,仍然不知道一些基本的东西。

    当我从JSON解析数据时,总是遇到一些必须转换为字符串的数字。所有数字都变为零。为什么?

    class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var titlenews = ""
    var title_segue = ""
    var id_segue = ""
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "newsfeedCell", for: indexPath) as! NewsFeedCell
        cell.newsfeed_title.text = self.news?[indexPath.item].headline
        cell.newsfeed_topic.text = self.news?[indexPath.item].topic
        cell.newsfeed_time.text = timetime(from: (self.news?[indexPath.item].time)!)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Launch tableview in SecondView")
        title_segue = (self.news?[indexPath.item].headline)!
        id_segue = (self.news?[indexPath.item].id)!
        print(("tableView: "+(self.news?[indexPath.item].headline)!))
        performSegue(withIdentifier: "newsbodysegue", sender: self)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    } 
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.news!.count
    } //number of rows
    
    @IBOutlet weak var tableview: UITableView!
    var news: [Newsfeed]? = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        getJSON()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func getJSON(){
    
        let urlRequest = URLRequest(url: URL(string: "https://sportarena.com/wp-api/wp/v2/posts/?per_page=25")!)
    
        let task = URLSession.shared.dataTask(with: urlRequest) { (data,response,error) in
    
            if error != nil {
                print(error as Any)
                return
            }
            self.news = [Newsfeed]()
            do {
                let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! NSArray
    
                for arrayX in json as! [[String: Any]] {
                   let NF = Newsfeed()
    
                    let categories0 = arrayX["categories"] as? NSArray
                    let title0 = arrayX["title"] as? [String: Any]
    
                    if let ID = arrayX["id"],
                        let date = arrayX["date"],
                        let status = arrayX["status"],
                        let title = title0!["rendered"],
                        let categories = categories0?[0] {
                            NF.headline = title as? String
                            NF.topic = status as? String
                            print (ID)
                            NF.id = ID as? String
                            NF.time = date as? String
                    }
                    self.news?.append(NF)
                 }
                DispatchQueue.main.async {
                    self.tableview.reloadData()
                }
            } catch let error {
                print(error)
            }
        }
        task.resume()
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "newsbodysegue") {
            let NewsBodyController = segue.destination as! NewsBody
            NewsBodyController.labeltext = id_segue
        }
    }
    
    }
    

    行“id\u segue=(self.news?[indexPath.item].id)!”给定错误:在展开可选值时意外发现nil。

    为什么nil if print(ID)显示从JSON解析的正确数字?

    0 回复  |  直到 6 年前