I am attempting to load this JSON data into a SwiftData Model:
{
"1": {
"id": "1",
"breed": "Bulldog Francés",
"fur": "Corto",
"furColor": "Gris",
"gender": "Hembra",
"imageUrl": "https://http2.mlstatic.com/D_NQ_NP_714611-MEC50253081381_062022-O.jpg",
"lifeExpectancy": 10
},
"2": {
"id": "2",
"breed": "french poodle",
"fur": "Corto",
"furColor": "Blanco",
"gender": "Macho",
"imageUrl": "https://http2.mlstatic.com/D_NQ_NP_693633-MEC50152204932_052022-O.jpg",
"lifeExpectancy": 16
},
}
I have Created a model for the Data:
import Foundation
import SwiftData
//model for an animal card
@Model
class Dog: Codable, Identifiable {
enum CodingKeys: CodingKey {
case id
case breed
case fur
case furColor
case gender
case imageUrl
case lifeExpectancy
}
var id: String
var breed: String
var fur: String
var furColor: String
var gender: String
var imageUrl: String
var lifeExpectancy: Int
init(id: String,breed: String, fur: String, furColor: String, gender: String, imageUrl: String, lifeExpectancy: Int) {
self.id = id
self.breed = breed
self.fur = fur
self.furColor = furColor
self.gender = gender
self.imageUrl = imageUrl
self.lifeExpectancy = lifeExpectancy
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
breed = try container.decode(String.self, forKey: .breed)
fur = try container.decode(String.self, forKey: .fur)
furColor = try container.decode(String.self, forKey: .furColor)
gender = try container.decode(String.self, forKey: .gender)
imageUrl = try container.decode(String.self, forKey: .imageUrl)
lifeExpectancy = try container.decode(Int.self, forKey: .lifeExpectancy)
}
func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(breed, forKey: .breed)
try container.encode(fur, forKey: .fur)
try container.encode(furColor, forKey: .furColor)
try container.encode(gender, forKey: .gender)
try container.encode(imageUrl, forKey: .imageUrl)
try container.encode(lifeExpectancy, forKey: .lifeExpectancy)
}
}
And then I am attempting to load it In but I am getting Failed to pre-seed database printed, I have confirmed the Url is correct so I am unsure what could be the problem:
@main
struct AnimalGuesserApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(for: Dog.self) { result in
do {
let container = try result.get()
// Check we haven't already added our users.
let descriptor = FetchDescriptor<Dog>()
let existingDogs = try container.mainContext.fetchCount(descriptor)
guard existingDogs == 0 else { return }
// Load and decode the JSON.
guard let url = Bundle.main.url(forResource: "dogs", withExtension: "json") else {
fatalError("Failed to find dogs.json")
}
print("URL:", url)
let data = try Data(contentsOf: url)
let dogs = try JSONDecoder().decode([Dog].self, from: data)
print(dogs)
// Add all our data to the context.
for dog in dogs {
print(dog.breed)
container.mainContext.insert(dog)
}
} catch {
print("Failed to pre-seed database.")
}
}
}
}
I am sure the json is added to the target and the name is correct but loading the contents of the url just seems to throw an exception.