Good afternoon, y'all
Given
Routine (Entity)
Relationship: weekdays, Destination: Weekday, Inverse: routine
Weekday (Entity)
Attribute: int (Integer 64)
Relationship: routine, Destination: Routine, Inverse: weekdays
Objective
Filter all routines with a relationship, weekdays, that contains a Weekday whose attribute int is equal to an Integer64 that you specify.
My Attempts so far
// Constant
let weekDay = 1
// Attempt 1
let attemptedNSPredicate = NSPredicate(format: "ANY weekdays.int == %@", weekDay)
// Attempt 2
let attemptedNSPredicate = NSPredicate(format: "SUBQUERY(weekdays, weekday, weekday.int == %@", weekday)
// Attempt 3
let attemptedNSPredicate = NSPredicate(format: "ANY %K.int == %@", \Routine.weekdays, weekday)
What is your solution to this problem
func yourSolution(_ solution: NSPredicate) -> FetchRequset {
FetchRequest<Routine>(
entity: Routine.entity(),
sortDescriptors: [],
predicate: solution
)
)
If weekdays is an Int64 attribute, then you should tell the format to use it like that -> %ld instead of %@.
So #3 would be the right one to use with that little modification:
let attemptedNSPredicate = NSPredicate(format: "ANY %K.int == %ld", \Routine.weekdays, weekday)
In your case the relationship does not make too much sense though. Weekdays is a finite set. So making it an enum and using it as a property of Routine directly would make things a lot simpler and faster.