加载本地 json 文件时更新时间超过 5 秒
Updating took more than 5 seconds when loading local json file
我试图从本地 json 文件加载一些数据,但它一直显示
"PreviewUpdateTimedOutError: Updating took more than 5 seconds
这让我可以恢复了canvas。我不知道我的结构中的错误是什么。
json.file 从 api
下载
我的加载json函数:
func loadJsonData() -> [Movie] {
let filename : String="data.json"
guard let url = Bundle.main.url(forResource: filename, withExtension: nil)else{
fatalError("no file found")}
guard let data = try? Data(contentsOf: url) else {
fatalError("can't load") }
guard let list = try? JSONDecoder().decode(movieData.self, from: data) else {
fatalError("no file found") }
return list.result
}
我的结构文件:
struct movieData:Codable {
var result:[Movie]
}
struct Movie:Codable,Identifiable{
var adult:Bool
var backdrop_path:String
var genre_ids:[Int]
var id:Int
var original_language:String
var original_title:String
var overview:String
var popularity:Double
var poster_path:String?
var release_data:String
var title:String?
var video:Bool
var vote_average:Double?
var vote_count:Int
}
我的data.json
{
"result": [
{
"adult": false,
"backdrop_path": "/yizL4cEKsVvl17Wc1mGEIrQtM2F.jpg",
"genre_ids": [
28,
878,
12
],
"id": 588228,
"original_language": "en",
"original_title": "The Tomorrow War",
"overview": "The world is stunned when a group of time travelers arrive from the year 2051 to deliver an urgent message: Thirty years in the future, mankind is losing a global war against a deadly alien species. The only hope for survival is for soldiers and civilians from the present to be transported to the future and join the fight. Among those recruited is high school teacher and family man Dan Forester. Determined to save the world for his young daughter, Dan teams up with a brilliant scientist and his estranged father in a desperate quest to rewrite the fate of the planet.",
"popularity": 3368.737,
"poster_path": "/34nDCQZwaEvsy4CFO5hkGRFDCVU.jpg",
"release_date": "2021-09-03",
"title": "The Tomorrow War",
"video": false,
"vote_average": 7.8,
"vote_count": 271
},
}
我的内容视图
import SwiftUI
struct HomeView: View {
let moviedata: movieData
let movies:[Movie]
init(){
self.moviedata = movieData(result: loadJsonData())
movies=self.moviedata.result
}
var body: some View {
Text("hello!?")
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
在您的 struct Movie
更改中
var release_data:String
至
var release_date:String
解码器在 guard 语句中发送致命错误,因为
您的 json 文件在数组末尾丢失 ],导致视图崩溃。
并且在书中结构中将 releaseData 更改为 releaseDate
var releaseData : String
到
var releaseDate : String
data.json :
{
"result":
[
{
"adult": false,
"backdrop_path": "/yizL4cEKsVvl17Wc1mGEIrQtM2F.jpg",
"genre_ids": [
28,
878,
12
],
"id": 588228,
"original_language": "en",
"original_title": "The Tomorrow War",
"overview": "The world is stunned when a group of time travelers arrive from the year 2051 to deliver an urgent message: Thirty years in the future, mankind is losing a global war against a deadly alien species. The only hope for survival is for soldiers and civilians from the present to be transported to the future and join the fight. Among those recruited is high school teacher and family man Dan Forester. Determined to save the world for his young daughter, Dan teams up with a brilliant scientist and his estranged father in a desperate quest to rewrite the fate of the planet.",
"popularity": 3368.737,
"poster_path": "/34nDCQZwaEvsy4CFO5hkGRFDCVU.jpg",
"release_date": "2021-09-03",
"title": "The Tomorrow War",
"video": false,
"vote_average": 7.8,
"vote_count": 271
}
]}
我试图从本地 json 文件加载一些数据,但它一直显示
"PreviewUpdateTimedOutError: Updating took more than 5 seconds
这让我可以恢复了canvas。我不知道我的结构中的错误是什么。 json.file 从 api
下载我的加载json函数:
func loadJsonData() -> [Movie] {
let filename : String="data.json"
guard let url = Bundle.main.url(forResource: filename, withExtension: nil)else{
fatalError("no file found")}
guard let data = try? Data(contentsOf: url) else {
fatalError("can't load") }
guard let list = try? JSONDecoder().decode(movieData.self, from: data) else {
fatalError("no file found") }
return list.result
}
我的结构文件:
struct movieData:Codable {
var result:[Movie]
}
struct Movie:Codable,Identifiable{
var adult:Bool
var backdrop_path:String
var genre_ids:[Int]
var id:Int
var original_language:String
var original_title:String
var overview:String
var popularity:Double
var poster_path:String?
var release_data:String
var title:String?
var video:Bool
var vote_average:Double?
var vote_count:Int
}
我的data.json
{
"result": [
{
"adult": false,
"backdrop_path": "/yizL4cEKsVvl17Wc1mGEIrQtM2F.jpg",
"genre_ids": [
28,
878,
12
],
"id": 588228,
"original_language": "en",
"original_title": "The Tomorrow War",
"overview": "The world is stunned when a group of time travelers arrive from the year 2051 to deliver an urgent message: Thirty years in the future, mankind is losing a global war against a deadly alien species. The only hope for survival is for soldiers and civilians from the present to be transported to the future and join the fight. Among those recruited is high school teacher and family man Dan Forester. Determined to save the world for his young daughter, Dan teams up with a brilliant scientist and his estranged father in a desperate quest to rewrite the fate of the planet.",
"popularity": 3368.737,
"poster_path": "/34nDCQZwaEvsy4CFO5hkGRFDCVU.jpg",
"release_date": "2021-09-03",
"title": "The Tomorrow War",
"video": false,
"vote_average": 7.8,
"vote_count": 271
},
}
我的内容视图
import SwiftUI
struct HomeView: View {
let moviedata: movieData
let movies:[Movie]
init(){
self.moviedata = movieData(result: loadJsonData())
movies=self.moviedata.result
}
var body: some View {
Text("hello!?")
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
}
}
在您的 struct Movie
更改中
var release_data:String
至
var release_date:String
解码器在 guard 语句中发送致命错误,因为 您的 json 文件在数组末尾丢失 ],导致视图崩溃。
并且在书中结构中将 releaseData 更改为 releaseDate
var releaseData : String
到
var releaseDate : String
data.json :
{
"result":
[
{
"adult": false,
"backdrop_path": "/yizL4cEKsVvl17Wc1mGEIrQtM2F.jpg",
"genre_ids": [
28,
878,
12
],
"id": 588228,
"original_language": "en",
"original_title": "The Tomorrow War",
"overview": "The world is stunned when a group of time travelers arrive from the year 2051 to deliver an urgent message: Thirty years in the future, mankind is losing a global war against a deadly alien species. The only hope for survival is for soldiers and civilians from the present to be transported to the future and join the fight. Among those recruited is high school teacher and family man Dan Forester. Determined to save the world for his young daughter, Dan teams up with a brilliant scientist and his estranged father in a desperate quest to rewrite the fate of the planet.",
"popularity": 3368.737,
"poster_path": "/34nDCQZwaEvsy4CFO5hkGRFDCVU.jpg",
"release_date": "2021-09-03",
"title": "The Tomorrow War",
"video": false,
"vote_average": 7.8,
"vote_count": 271
}
]}