开发者

How can I detect the day of the week and the time, and then display data in a tableview based on the current day and time?

开发者 https://www.devze.com 2022-12-21 19:42 出处:网络
I have a list of data that is a schedule.Each item has a time that i开发者_开发问答t takes place in.I want to detect the current day and time and then display what items are available during that time

I have a list of data that is a schedule. Each item has a time that i开发者_开发问答t 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消