I am a complete SwiftUI beginner. I have gone through Apple's Landmarks tutorial, and a bunch of the videos here. I am now trying to render out a view with some alternative JSON as the data.
However, when I try and use that data I get a Fatal Error in ModelData.swift - crashed due to fatalError in ModelData.swift at line 40
The logs don't show me anything I understand, they don't even reference that same message so I am lost.
I thought it must be something to do with how I have mapped the data. Here is my mapping of the JSON called Competition.swift,
followed by the JSON, followed by my simple view.
Hope somebody can shed some light on what I have wrong here?
import Foundation
import SwiftUI
struct Competition: Codable, Identifiable {
var id: UUID
var _id: String
var title: String
var titleShort: String
var classification: Classification
var fixtures: [FixtureSingle]
struct Classification: Codable {
var title: String
var bId: Int
}
struct FixtureSingle: Codable {
var _id: String
var title: String
var date: String
var venue: String
var classification: Classification
var competition: Competition
var referee: Referee
var participants: [Participant]
var previousMeetings: PreviousMeeting
}
struct Referee: Codable {
var titleShort: String
var nationality: String
var title: String
}
struct Participant: Codable {
var titleShort: String
var title: String
var _id: String
var kits: [Kit]
var logo: String
var nationality: String
var preMatchStats: PreMatchStat
var classification: Classification
var form: [Form]
}
struct Kit: Codable {
var asset: String
var mainColour: String
}
struct PreMatchStat: Codable {
var leadingScorer: LeadingScorer
var leaguePosition: Int
var cleanSheets: Double
var atLeastOneGoal: Double
var goalsScoredAverage: Double
var goalsConcededAverage: Double
var goalDifference: Int
var winPercentage: Double
}
struct LeadingScorer: Codable {
var totalGoals: Int
var title: String
var titleShort: String
}
struct Form: Codable {
var date: String
var titleShort: String
var scoreline: [String]
var winner: Int
var participants: [FormParticipant]
}
struct FormParticipant: Codable {
var _id: String
}
struct PreviousMeeting: Codable {
var titleShort: String
var winner: Int
var date: Date
var scoreline: [String]
var competition: Competition
var participants: [Participant]
}
}
And here is the JSON file:
[{
"_id": "6304f394d05afb456f03d654",
"title": "England Premier League",
"titleShort": "EPL",
"classification": {
"title": "Soccer",
"bId": 1
},
"fixtures": [
{
"_id": "6304f394d05afb456f03d656",
"title": "Tottenham v Wolverhampton",
"date": "Sat Aug 20 2022 12:30:00 GMT+0100 (British Summer Time)",
"venue": "Tottenham Hotspur Stadium",
"classification": {
"bId": 1,
"title": "Soccer"
},
"competition": {
"_id": "6304f394d05afb456f03d654",
"title": "England Premier League"
},
"referee": {
"titleShort": "S Hooper",
"nationality": "England",
"title": "Simon Hooper"
},
"participants": [
{
"titleShort": "TOT",
"title": "Tottenham",
"_id": "6304f392d05afb456f03285d",
"kits": [
{
"asset": "Tottenham_Hotspur_Home_23",
"mainColour": "#ffffff"
}
],
"preMatchStats": {
"leadingScorer": {
"totalGoals": 4,
"title": "Harry Kane",
"titleShort": "H Kane"
},
"leaguePosition": 4,
"cleanSheets": 0.71,
"atLeastOneGoal": 0.75,
"goalsScoredAverage": 2.29,
"goalsConcededAverage": 1.11,
"goalDifference": 33,
"winPercentage": 14.29
},
"classification": {
"bId": 1
},
"form": [
{
"date": "Sun Aug 14 2022 16:30:00 GMT+0100 (British Summer Time)",
"titleShort": "CHE v TOT",
"scoreline": [
"2-2"
],
"winner": -1,
"participants": [
{
"_id": "6304f392d05afb456f032a72"
},
{
"_id": "6304f392d05afb456f03285d"
}
]
}
]
},
{
"titleShort": "WOL",
"title": "Wolverhampton",
"_id": "6304f392d05afb456f032886",
"kits": [
{
"asset": "Wolverhampton_Wanderers_Home_23",
"mainColour": "#fecd32"
}
],
"preMatchStats": {
"leadingScorer": {
"totalGoals": 3,
"title": "Daniel Podence",
"titleShort": "D Podence"
},
"leaguePosition": 14,
"cleanSheets": 0.72,
"atLeastOneGoal": 0.72,
"goalsScoredAverage": 1.34,
"goalsConcededAverage": 1.24,
"goalDifference": 3,
"winPercentage": 17.24
},
"classification": {
"bId": 1
},
"form": [
{
"date": "Sat Aug 13 2022 15:00:00 GMT+0100 (British Summer Time)",
"titleShort": "WOL v FUL",
"scoreline": [
"0-0"
],
"winner": -1,
"participants": [
{
"_id": "6304f392d05afb456f032886"
},
{
"_id": "6304f392d05afb456f032953"
}
]
},
]
}
],
"previousMeetings": [
{
"titleShort": "TOT v WOL",
"winner": 1,
"date": "Sun Feb 13 2022 14:00:00 GMT+0000 (Greenwich Mean Time)",
"scoreline": [
"0-2"
],
"competition": {
"titleShort": "EPL"
},
"participants": [
{
"titleShort": "TOT",
"_id": "6304f392d05afb456f03285d"
},
{
"titleShort": "WOL",
"_id": "6304f392d05afb456f032886"
}
]
},
{
"titleShort": "WOL v TOT",
"winner": 1,
"date": "Wed Sep 22 2021 19:45:00 GMT+0100 (British Summer Time)",
"scoreline": [
"2-2"
],
"competition": {
"titleShort": "EFL"
},
"participants": [
{
"titleShort": "WOL",
"_id": "6304f392d05afb456f032886"
},
{
"titleShort": "TOT",
"_id": "6304f392d05afb456f03285d"
}
]
},
{
"titleShort": "WOL v TOT",
"winner": 1,
"date": "Sun Aug 22 2021 14:00:00 GMT+0100 (British Summer Time)",
"scoreline": [
"0-1"
],
"competition": {
"titleShort": "EPL"
},
"participants": [
{
"titleShort": "WOL",
"_id": "6304f392d05afb456f032886"
},
{
"titleShort": "TOT",
"_id": "6304f392d05afb456f03285d"
}
]
},
{
"titleShort": "TOT v WOL",
"winner": 0,
"date": "Sun May 16 2021 14:05:00 GMT+0100 (British Summer Time)",
"scoreline": [
"2-0"
],
"competition": {
"titleShort": "EPL"
},
"participants": [
{
"titleShort": "TOT",
"_id": "6304f392d05afb456f03285d"
},
{
"titleShort": "WOL",
"_id": "6304f392d05afb456f032886"
}
]
},
{
"titleShort": "WOL v TOT",
"winner": -1,
"date": "Sun Dec 27 2020 19:15:00 GMT+0000 (Greenwich Mean Time)",
"scoreline": [
"1-1"
],
"competition": {
"titleShort": "EPL"
},
"participants": [
{
"titleShort": "WOL",
"_id": "6304f392d05afb456f032886"
},
{
"titleShort": "TOT",
"_id": "6304f392d05afb456f03285d"
}
]
}
]
}
]
}]
And here is the view that won't load/preview:
import SwiftUI
struct FixtureList: View {
@Environment(ModelData.self) var modelData
var body: some View {
Text("hello")
}
}
#Preview {
FixtureList().environment(ModelData())
}