I am relatively new to Swift programming (for ~ 2 months). I have created a simple SwiftData schema made up of the following tables:
ActivityType
Activity
ChallengeGroup
Challenges
@Model
final class ActivityType: Identifiable, Hashable {
@Attribute(.unique) var activityTypeName: String
var activityTypeIcon: String
@Relationship(inverse: Activity.activityType) var activities: [Activity]
@Model
final class Activity: Identifiable, Hashable {
@Attribute(.unique) var id: Int
var activityName: String
@Relationship(inverse: ChallengeGroup.activity) var challengeGroups: [ChallengeGroup]
var activityType: ActivityType?
@Model
final class ChallengeGroup: Identifiable, Hashable {
@Attribute(.unique) var id: Int
var challengeGroupName: String
var category: String
@Relationship(inverse: Challenge.challengeGroup) var challenges: [Challenge]
var activity: Activity?
@Model
final class Challenge: Identifiable, Hashable {
@Attribute(.unique) var id: Int
var challengeName: String
var challengeGroup: [ChallengeGroup]
I am having a problem trying to obtain all Challenges for a particular Activity.
I have tried declaring within a struct:
@Query var challenges: [Challenge]
Then within the init():
init(for activity: Activity) {
let id = activity.id
self._challenges = Query(filter: #Predicate {
$0.challengeGroup.activity?.id == id. <== error here
}, sort: .challengeName)
self.activity = activity
}
I am receiving the error of “Value of type ‘[ChallengeGroup]’ has no member ‘activity'” – which makes sense as ChallengeGroup is declared as an array of ChallengeGroups. So if I try:
init(for activity: Activity) {
let id = activity.id
self._challenges = Query(filter: #Predicate {
$0.[challengeGroup].activity?.id == id. <== error here
}, sort: .challengeName)
self.activity = activity
}
I have now added square braces around the name, I receive the error: Expected member name following ‘.’
I am really stuck with this and have no idea how to proceed – it seems like Catch 22. Are you able to provide some solutions to my problem?
Many thanks.
newbie987654321 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.