"'Element' is not a member type of class" error in SwiftData: What am I doing wrong?

I am trying to use a SwiftData model but I keep getting the error for the code below.

'Element' is not a member type of class MyModel

Is this a bug or am I missing something?

@Model
final class MyModel {
    var timestamp: Date
    var favorites: [String]
    
    init(timestamp: Date, favorites: [String]) {
        self.timestamp = timestamp
        self.favorites = favorites
    }
    
    public func remove(at index: Int) {
        favorites.remove(at: index)
    }
    public func add(symbol: String) {
        favorites.append(symbol)
    }
}

public struct DetailView: View {
    @Environment(\.modelContext) private var modelContext
    @Query var model: MyModel
    
    public var body: some View {
        VStack { ... }
    }
}

SwiftData was never meant to be used like this. I recommend reading the Apple SwiftData examples and watching the WWDC videos.

instead of @Query var model: MyModel, did you mean @Query var models: [MyModel]? a Query returns an array.

I am getting the same error at various places using./Users/ronjurincie/Desktop/Screenshot 2024-02-08 at 8.49.15 PM.png @Query var appState: AppState

AppState is NOT an array and the identical call works in 80% of the Views????

Could this be a [BUG??]

@Query will return an array of results. Similar to using fetch from a managed object context.

Changing your code to this should work: @Query var model: [MyModel]

"'Element' is not a member type of class" error in SwiftData: What am I doing wrong?
 
 
Q