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
现在我遇到的问题是我不知道是什么引发了此错误。
我只能认为它是在脚本的新闻部分内发生的,因为当我从一个屏幕转到另一个屏幕时,它不是在原始脚本中发生的。
我知道这是在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()
}
关于ios - 以未捕获的异常终止:NSException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59083953/