Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you want to group by weekday, the easiest thing to do is to add an attribute such as "weekday" to your entity; then you pass "weekday" as sectionNameKeyPath argument when initializing the NSFetchedResultsController. In this case there is no need to specify a predicate: you will automatically get sections for the weekdays.</p> <p>If you want to group by day, you pass DateAttribute as sectionNameKeyPath argument when initializing the NSFetchedResultsController. Again, there is no need to specify a predicate: you will automatically get sections for the days.</p> <p>What you are trying to do in your code differs from what you asked for. Indeed, you are not trying to get back sections (i.e. grouping by an attribute): you are instead trying to get back a single section for your tableView containing objects described by your entity satisfying a specific predicate, namely the ones whose DateAttribute falls (probably) in the current day. To achieve this, you can use the following code:</p> <pre><code>// start by retrieving day, weekday, month and year components for today NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]]; NSInteger theDay = [todayComponents day]; NSInteger theMonth = [todayComponents month]; NSInteger theYear = [todayComponents year]; // now build a NSDate object for the input date using these components NSDateComponents *components = [[NSDateComponents alloc] init]; [components setDay:theDay]; [components setMonth:theMonth]; [components setYear:theYear]; NSDate *thisDate = [gregorian dateFromComponents:components]; [components release]; // now build a NSDate object for tomorrow NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; [offsetComponents setDay:1]; NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0]; [offsetComponents release]; NSDateComponents *tomorrowComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:nextDate]; NSInteger tomorrowDay = [tomorrowComponents day]; NSInteger tomorrowMonth = [tomorrowComponents month]; NSInteger tomorrowYear = [tomorrowComponents year]; [gregorian release]; // now build the predicate needed to fetch the information NSPredicate *predicate = [NSPredicate predicateWithFormat: @"DateAttribute &lt; %@ &amp;&amp; DateAttribute &gt; %@", nextDate, thisDate]; </code></pre> <p>This predicate will retrieve exactly all of the objects whose DateAttribute falls within the current day. Hope this helps.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload