我有这样的NSDictionary数组。

 {
    id = 12;
    "time_from" = "12:00";
    "time_to" = "16:00";
    "timing_name" = Lunch;
},
    {
    id = 11;
    "time_from" = "08:00";
    "time_to" = "12:00";
    "timing_name" = BreakFast;
},
    {
    id = 13;
    "time_from" = "09:00";
    "time_to" = "10:00";
    "timing_name" = Dinner;
}


假设我的当前时间是8:45,那么如果当前时间在time_from和time_to之间退出,我们如何使用谓词找到确切的字典。

实际上,我需要一个谓词。

最佳答案

你可以试试看

NSMutableArray *testArray = [NSMutableArray array];
    NSMutableDictionary *testDict = [NSMutableDictionary dictionary];
    [testDict setObject:@"12:45" forKey:@"time-from"];
    [testDict setObject:@"14:45" forKey:@"time-to"];
    [testArray addObject:testDict];

   NSPredicate *timePredicate = [NSPredicate predicateWithBlock:^BOOL(NSMutableDictionary *dict, NSDictionary *bindings) {

        NSString *timeFromString = [dict objectForKey:@"time-from"];
        NSString *timeToString = [dict objectForKey:@"time-from"];

        // find minutes from 00-0
        //get current time minutes from 00-00
        return (currentTimeMinutes>startimeMinutes) && (currentTimeMinutes<=endtimeMinutes)


    }];

    [testArray filterUsingPredicate:timePredicate];

关于iphone - 如何比较两个时隙之间的当前时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16734151/

10-10 22:23