@Query with Set

How do I filter data using @Query with a Set of DateComponents? I successfully saved multiple dates using a MultiDatePicker in AddView.swift. In ListView.swift, I want to retrieve all records for the current or today’s date.

There are hundreds of examples using @Query with strings and dates, but I haven’t found an example of @Query using a Set of DateComponents

Nothing will compile and after hundreds and hundreds of attempts, my hair is turning gray.

Please, please, please help me.

For example, if the current date is Tuesday, March 4 205, then I want to retrieve both records. Since both records contain Tuesday, March 4, then retrieve both records. Sorting works fine because the order by clause uses period which is a Double.

Unfortunately, my syntax is incorrect and I don’t know the correct predicate syntax for @Query and a Set of DateComponents.

Class Planner.swift file

import SwiftUI
import SwiftData


@Model
class Planner {
    
    //var id: UUID = UUID()
    var grade: Double = 4.0
    var kumi: Double = 4.0
    var period: Double = 1.0
   
    var dates: Set<DateComponents> = []
    
    init(
        grade: Double = 4.0, kumi: Double = 4.0, period: Double = 1.0, dates: Set<DateComponents> = []
        )
    {
        self.grade = grade
        self.kumi = kumi
        self.period = period
        self.dates = dates


    }
}

@Query Model snippet of code does not work The compile error is to use a Set of DateComponents, not just DateComponents.

@Query(filter: #Predicate<Planner> { $0.dates = DateComponents(calendar: Calendar.current, year: 2025, month: 3, day: 4)},
           sort: [SortDescriptor(\Planner.period)])
    var planner: [Planner]

ListView.swift image

EditView.swift for record #1

DB Browser for SQLlite: record #1 (March 6, 2025 and March 4, 2025) 



 [{"isLeapMonth":false,"year":2025,"day":6,"month":3,"calendar":{"identifier":"gregorian","minimumDaysInFirstWeek":1,"current":1,"locale":{"identifier":"en_JP","current":1},"firstWeekday":1,"timeZone":{"identifier":"Asia\/Tokyo"}},"era":1},{"month":3,"year":2025,"day":4,"isLeapMonth":false,"era":1,"calendar":{"locale":{"identifier":"en_JP","current":1},"timeZone":{"identifier":"Asia\/Tokyo"},"current":1,"identifier":"gregorian","firstWeekday":1,"minimumDaysInFirstWeek":1}}]


EditView.swift for record #2

DB Browser for SQLlite: record #2 (March 3, 2025 and March 4, 2025) 


[{"calendar":{"minimumDaysInFirstWeek":1,"locale":{"current":1,"identifier":"en_JP"},"timeZone":{"identifier":"Asia\/Tokyo"},"firstWeekday":1,"current":1,"identifier":"gregorian"},"month":3,"day":3,"isLeapMonth":false,"year":2025,"era":1},{"year":2025,"month":3,"era":1,"day":4,"isLeapMonth":false,"calendar":{"identifier":"gregorian","current":1,"firstWeekday":1,"minimumDaysInFirstWeek":1,"timeZone":{"identifier":"Asia\/Tokyo"},"locale":{"current":1,"identifier":"en_JP"}}}]
 


Any help is greatly appreciated.

Answered by DTS Engineer in 830864022

@Query(filter: #Predicate<Planner> { $0.dates = DateComponents(calendar: Calendar.current, year: 2025, month: 3, day: 4)},

Your code snippet shows that dates is a set, and so you'd want to check if the set contains (rather than is equal to) the DateComponents object, as shown below:

@Query(filter: #Predicate<Planner> { 
    $0.dates.contains(DateComponents(calendar: Calendar.current, year: 2025, month: 3, day: 4)
}

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

@Query(filter: #Predicate<Planner> { $0.dates = DateComponents(calendar: Calendar.current, year: 2025, month: 3, day: 4)},

Your code snippet shows that dates is a set, and so you'd want to check if the set contains (rather than is equal to) the DateComponents object, as shown below:

@Query(filter: #Predicate<Planner> { 
    $0.dates.contains(DateComponents(calendar: Calendar.current, year: 2025, month: 3, day: 4)
}

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

&#64;Query with Set
 
 
Q