I have a list of data that is a schedule. Each item has a time that it takes place in. I want to detect the current day and time and then display what items are available during that time. All I really know is how to get todays date and time and that I need to create a method to look in my data at what is currently "playing". Any suggestions?
views:
32answers:
1
+2
A:
By assuming that your schedule items are stored in an NSArray
called scheduleItems
and that these items have a date
property, you could filter them with a predicate:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date == %@",
[NSDate date]];
NSArray *todaysItems = [scheduleItems filteredArrayUsingPredicate:predicate];
The problem is that this will give you every item where its date is exactly now. You probably want to compare the date in a range:
NSDate *today = ...;
NSDate *tomorrow = ...;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date BETWEEN %@",
[NSArray arrayWithObjects:today, tomorrow,nil]];
NSArray *todaysItems = [scheduleItems fileteredArrayUsingPredicate:predicate];
Note: this is not tested.
Martin Cote
2010-03-04 19:26:56