Similar issue! The joys of coding.
Post
Replies
Boosts
Views
Activity
I think your question should be a class if you want to persist it in SwiftData. I think the general advice (for SwiftData) is: structs are for views, classes are for models.
Your models would be something like this:
@Model
final class Exam {
let timestamp: Date
let id: String
var title: String
@Relationship(deleteRule: .cascade, inverse: \Question.exam) var questions: [Question] = []
init(title:String = "") {
self.timestamp = .now
self.id = UUID().uuidString
self.title = title
}
}
@Model
final class Question {
let id: String
var number: Int
var points: Int
var prompt: String
var answer: String
@Relationship var exam: Exam? = nil
init(
number: Int = 0,
points: Int = 0,
prompt: String = "",
answer: String = ""
) {
self.id = UUID().uuidString
self.number = number
self.points = points
self.prompt = prompt
self.answer = answer
}
}
I hope this helps!