Hello everybody,
I'm quite new to SwiftUI and particularly to SwiftData related projects.
Now, I have the following two classes:
import Foundation
import SwiftData
@Model
class Stagione {
@Attribute(.unique) var idStagione: String
var categoriaStagione: String
var miaSquadra: String
@Relationship(deleteRule: .cascade) var rosa: [Rosa]?
@Relationship(deleteRule: .cascade) var squadra: [Squadre]?
@Relationship(deleteRule: .cascade) var partita: [CalendarioPartite]?
init(idStagione: String, categoriaStagione: String, miaSquadra: String) {
self.idStagione = idStagione
self.categoriaStagione = categoriaStagione
self.miaSquadra = miaSquadra
}
}
and
import Foundation
import SwiftData
@Model
class CalendarioPartite {
var idGiornata: Int
var dataPartita: Date
var squadraCasa: String
var squadraTrasferta: String
var golCasa: Int
var golTrasferta: Int
var stagione: Stagione?
init(idGiornata: Int, dataPartita: Date, squadraCasa: String, squadraTrasferta: String, golCasa: Int, golTrasferta: Int) {
self.idGiornata = idGiornata
self.dataPartita = dataPartita
self.squadraCasa = squadraCasa
self.squadraTrasferta = squadraTrasferta
self.golCasa = golCasa
self.golTrasferta = golTrasferta
}
}
Now, I want to create a view where, depending on a selectedStagione variable, I'd like to display all the related partite from CalendarioPartite.
Wrote the following query, but it retrieves me an error saying: "Instance member 'selectedStagione' cannot be used on type 'CalendarioCampionatoView'; did you mean to use a value of this type instead?". The query is:
@Query(filter: #Predicate<CalendarioPartite> { $0.stagione == selectedStagione}) private var partite: [CalendarioPartite] = []
Sorry for the basic question, but as I told you I'm quite a beginner with this language.
Thanks in advance for the support,
A.
vns
2
Hi! Welcome to the forum!
At first, I want to emphasise that SwiftData and SwiftUI is not a part of the language, but Apple frameworks written in Swift and utilising lot of its, sometimes advanced, features. In particular, you are here have a deal with instance & type members of a type, plus Query macro, one of the most recent complex additions to the capabilities of the language. If you want to understand more about how it works, I’d suggest learning it in a free from frameworks space, where only Swift and its standard library is available.
As for the issue itself, it is not supported right now, here is a workaround - https://www.hackingwithswift.com/quick-start/swiftdata/how-to-dynamically-change-a-querys-sort-order-or-predicate