Accessing array field with SwiftData throws exception

With the following code:

@Model
final class Item {
    var data: [Data]
    
    init(data: [Data]) {
        self.data = data
    }
}

@Model
final class Data {
    var contents: String

    init(contents: String) {
        self.contents = contents
    }
}

..

let data = Data(contents: "yo")
let newItem = Item(timestamp: Date(), data: [data])
print(newItem.data) // <-- exception thrown here

I get an exception on the print line when accessing the .data field:

Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1f379a29c)

Note that if I first save newItem to the context the exception is not thrown, i.e.:

let data = Data(contents: "yo")
let newItem = Item(timestamp: Date(), data: [data])
modelContext.insert(newItem)
print(newItem.data) // <-- no exception

Unfortunately the exception is cryptic so I can't tell what's wrong.


Xcode version 15.0 beta 8 (15A5229m)

macOS Sonoma beta 14.0 (23A5337a)

Same issue

Seems to be a bug, this code gives me a similar compiler error

import Foundation import SwiftUI import SwiftData

// DataTopModelClass.swift // Oracle AI // // Created by Steven Ebrey on 29/08/2023. //

@Model

final class TopModel: ObservableObject {

var id: UUID

var currentExpertise: Expertise var previousExpertise: [Expertise] init(id: UUID, currentExpertise: Expertise) { self.id = id self.currentExpertise = currentExpertise self.previousExpertise = [] }

}

extension TopModel: Identifiable {}

extension TopModel: Equatable {

static func == (lhs: TopModel, rhs: TopModel) -> Bool {
    return lhs.id == rhs.id && lhs.currentExpertise == rhs.currentExpertise && lhs.previousExpertise == rhs.previousExpertise
}

}

extension TopModel: Hashable { func hash(into hasher: inout Hasher) { hasher.combine(id) hasher.combine(currentExpertise) hasher.combine(previousExpertise) // hasher.combine(messagesmodel) } }

Here is compiler error

{ @storageRestrictions(accesses: _$backingData, initializes: _currentExpertise) init(initialValue) { _$backingData.setValue(forKey: .currentExpertise, to: initialValue) _currentExpertise = _SwiftDataNoType() }

get {
                _$observationRegistrar.access(self, keyPath: \.currentExpertise)
                return self.getValue(forKey: \.currentExpertise)
    }

set {
                _$observationRegistrar.withMutation(of: self, keyPath: \.currentExpertise) {
                        self.setValue(forKey: \.currentExpertise, to: newValue)
                }
    }

}

Same issue

Accessing array field with SwiftData throws exception
 
 
Q