How would I pass in sample data for a struct within a struct for Swift Previews? In my code, I have StudentClass struct with some properties. One of those properties is made of another struct called Assignment.
import Foundation
struct StudentClass: Identifiable {
let id: UUID
let title: String
let teacher: String
var grade: Float
var theme: Theme
var assignments: [Assignment]
init(id: UUID = UUID(), title: String, teacher: String, grade: Float, theme: Theme, assignments: [Assignment]) {
self.id = id
self.title = title
self.teacher = teacher
self.grade = grade
self.theme = theme
self.assignments = assignments
}
}
extension StudentClass {
struct Assignment: Identifiable, Codable {
let id: UUID
let name: String
var grade: Float
var pointsScored: Float
var totalPoints: Float
init(id: UUID = UUID(), name: String, grade: Float, pointsScored: Float, totalPoints: Float) {
self.id = id
self.name = name
self.grade = grade
self.pointsScored = pointsScored
self.totalPoints = totalPoints
}
}
}
So, while passing in sample data, I try passing in a list inside a list of sample properties, but it returns an error on my extension of sample data that says, "Type of expression is ambiguous without more context". Here is my sample data code:
extension StudentClass {
static let sampleData: [StudentClass] =
[
StudentClass(title: "Math", teacher: "Baski", grade: 100.00, theme: .yellow, assignments: [["Math HW", 100.00, 5.0, 5.0], ["Math HW #2", 90.00, 4.5, 5.0]]),
StudentClass(title: "English", teacher: "Ritch", grade: 90.55, theme: .orange, assignments: [["English HW", 100.00, 5.0, 5.0], ["English HW #2", 90.00, 4.5, 5.0]]),
StudentClass(title: "Global Studies", teacher: "Tiedemann", grade: 95.92, theme: .poppy, assignments: [["GS HW", 100.00, 5.0, 5.0], ["GS HW #2", 90.00, 4.5, 5.0]]),
StudentClass(title: "Science", teacher: "Bolash", grade: 95.14, theme: .teal, assignments: [["Science HW", 100.00, 5.0, 5.0], ["Science HW #2", 90.00, 4.5, 5.0]])
]
}
How can I fix this problem?