gpt4 book ai didi

ios - 以未捕获的异常 : NSException 终止

转载 作者:行者123 更新时间:2023-12-01 19:38:18 27 4
gpt4 key购买 nike

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 上的东西导致它触发。

Crash

这是新闻的 View Controller 脚本。
//
// 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com