Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以未捕获异常终止:NSException_Ios_Swift - Fatal编程技术网

Ios 以未捕获异常终止:NSException

Ios 以未捕获异常终止:NSException,ios,swift,Ios,Swift,Swift应用程序崩溃,并给我此错误 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.' *** Firs

Swift应用程序崩溃,并给我此错误

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.'
*** First throw call stack:
(0x1aa5a980c 0x1aa2d1fa4 0x1aaa7ff1c 0x1aa88ad64 0x1aa88ac7c 0x1aa88a8f0 0x1aeaa6c34 0x1aeaa766c 0x1b0ff578c 0x1b0ffb908 0x1b1006528 0x1b0f4eed0 0x1b0f78bbc 0x1b0f79b40 0x1aa2c9344 0x1aa2c622c 0x1aa2c7280 0x1aa2c7018 0x1aa2c9ad4)
libc++abi.dylib: terminating with uncaught exception of type NSException
现在的问题是我不知道是什么导致了这个错误

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.'
*** First throw call stack:
(0x1aa5a980c 0x1aa2d1fa4 0x1aaa7ff1c 0x1aa88ad64 0x1aa88ac7c 0x1aa88a8f0 0x1aeaa6c34 0x1aeaa766c 0x1b0ff578c 0x1b0ffb908 0x1b1006528 0x1b0f4eed0 0x1b0f78bbc 0x1b0f79b40 0x1aa2c9344 0x1aa2c622c 0x1aa2c7280 0x1aa2c7018 0x1aa2c9ad4)
libc++abi.dylib: terminating with uncaught exception of type NSException
我只能认为它发生在脚本的新闻部分,因为当我从一个屏幕转到另一个屏幕时,它没有发生在原始脚本中

我知道是newsViewController上的某个东西导致它起火

下面是新闻的视图控制器脚本

//
//  NewsViewController.swift
//  DRN1
//
//  Created by Russell Harrower on 26/11/19.
//  Copyright © 2019 Russell Harrower. All rights reserved.
//

import UIKit


struct NewsData: Decodable{
    let news: [articalData]
}

struct articalData: Decodable{
    let title: String
}

class NewsViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!

    var news: [News] = []

    override func viewDidLoad() {
      super.viewDidLoad()

        self.newsfetch { [weak self] news in
            guard let news = news else { return }
            self?.news = news
            self?.tableView.reloadData()
        }

        tableView.delegate = self
        tableView.dataSource = self
    }

    func createArray() -> [News] {

           return [News(title: "Hello") , News(title: "how") , News(title: "You")]

    }

    func newsfetch(_ completionHandler:  @escaping ([News]?)->Void){
        let jsonURLString = "https://api.drn1.com.au/api-access/news"
        guard let feedurl = URL(string: jsonURLString) else {  return }

        URLSession.shared.dataTask(with: feedurl){ (data,response,err)
            in
            guard let news = data else { return }
            do {
                let newsdata = try JSONDecoder().decode(NewsData.self, from: news)
                var tempNews: [News] = []
                newsdata.news.forEach(){
                    tempNews.append(News(title: $0.title))
                }
                completionHandler(tempNews)
            } catch let jsonErr {
                print("error json ", jsonErr)
                completionHandler(nil)
            }
        }.resume()

    }


    /*func newsfetch() -> [News]{

        var tempNews: [News] = []
                    let jsonURLString = "https://api.drn1.com.au/api-access/news"
                    guard let feedurl = URL(string: jsonURLString) else {  return tempNews }

                      URLSession.shared.dataTask(with: feedurl) { (data,response,err)
                          in

                          guard let news = data else { return }

                          do{
                            let newsdata = try JSONDecoder().decode(NewsData.self, from: news)

                            newsdata.news.forEach(){

                                DispatchQueue.main.async {
                                    print($0.title)

                                    tempNews.append(News(title: $0.title))
                                    print(tempNews.count)
                                }
                            }
                            }catch let jsonErr{

                                print("error json ", jsonErr)
                            }


                      }.resume()



        return tempNews

        }*/


  override func viewDidAppear(_ animated: Bool) {
    self.tabBarController?.navigationItem.title = "News"




  }

}


extension NewsViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return news.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let newsa = news[indexPath.row]

        let cell = tableView.dequeueReusableCell(withIdentifier: "NewsCell") as! NewsCell
        cell.setNews(news: newsa)
        return cell
    }
}

您需要在主线程上重新加载tableView。正如错误所暗示的,对布局引擎的修改应该始终通过主线程完成


将tableview.reloadData行更改为此行-

DispatchQueue.main.async { 
self?.tableView.reloadData() 
}

在主线程上重新加载表@如何更改?将tableview.reloadData行更改为此行-DispatchQueue.main.async{self.?.tableview.reloadData()}DispatchQueue.main.async{self.tableview.reloadData()}