Is it possible to create one-to-many relationships between optional objects in SwiftData Xcode Previews? The below code I call from #Preview
crashes the canvas on the post.authors?.append(tempAuthors.randomElement()!)
when I try to append an array to an optional array. I can append the previewAuthors
array to the authors
array in the simulator or on device, but it crashes in Preview. My running theory is it has something to do with being in memory since this works on device, but I'm not sure that I missed something super obvious.
static func previews(_ count: Int) -> [Post] {
let previewAuthors = [Author(name: "..."), Author(name: "...")]
var posts: [Post] = []
for i in 0..<count {
let post = Post(title: "Title \(i)")
post.authors?.append(previewAuthors.randomElement()!)
posts.append(post)
}
return posts
}