0

我有一个这样的 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 之间退出,我们如何使用谓词找到准确的字典。

实际上我需要一个谓词。

4

4 回答 4

1

你可以试试这个

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];
于 2013-05-24T12:00:42.997 回答
1

试试用这个...

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
    NSInteger currHr = [components hour];
    NSInteger currtMin = [components minute];

    NSString *startTime = @"10:00";
    NSString *endTime = @"16:00";

    int stHr = [[[startTime componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
    int stMin = [[[startTime componentsSeparatedByString:@":"] objectAtIndex:1] intValue];
    int enHr = [[[endTime componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
    int enMin = [[[endTime componentsSeparatedByString:@":"] objectAtIndex:1] intValue];

    int formStTime = (stHr*60)+stMin;
    int formEnTime = (enHr*60)+enMin;

    int nowTime = (currHr*60)+currtMin;

    if(nowTime >= formStTime && nowTime <= formEnTime)
    {
        // Do Some  Stuff..
    }
于 2013-05-24T12:01:19.500 回答
1
-(void)makeCalculation
{
NSMutableDictionary *temp=[[NSMutableDictionary alloc]init];

[temp setObject:@"13" forKey:@"id"];
[temp setObject:@"12:00" forKey:@"timeFrom"];
[temp setObject:@"16:00" forKey:@"timeTo"];

NSMutableDictionary *temp1=[[NSMutableDictionary alloc]init];

[temp1 setObject:@"13" forKey:@"id"];
[temp1 setObject:@"08:00" forKey:@"timeFrom"];
[temp1 setObject:@"12:00" forKey:@"timeTo"];

NSMutableDictionary *temp2=[[NSMutableDictionary alloc]init];

[temp2 setObject:@"13" forKey:@"id"];
[temp2 setObject:@"09:00" forKey:@"timeFrom"];
[temp2 setObject:@"10:00" forKey:@"timeTo"];

NSMutableArray *arrayOfDict=[[NSMutableArray alloc]init];

[arrayOfDict addObject:temp];
[arrayOfDict addObject:temp1];
[arrayOfDict addObject:temp2];


NSMutableArray *timeToCompare=[[NSMutableArray alloc]initWithObjects:@"8:45",@"8:45", nil];

for(int k=0;k<[arrayOfDict count];k++)
{
    NSMutableDictionary *temp=[arrayOfDict objectAtIndex:k];

    NSString *timeFrom=[temp objectForKey:@"timeFrom"];
    NSString *timeTo=[temp objectForKey:@"timeTo"];

    NSMutableArray *tempTocheck=[[NSMutableArray alloc]initWithObjects:timeFrom,timeTo, nil];
    if([self isTimeClash:timeToCompare compareWith:tempTocheck])
    {
        NSLog(@"Time Belong in id:%@",[temp objectForKey:@"id"]);

    }
}
}


-(BOOL)isTimeClash:(NSMutableArray *)timeToCheck compareWith:(NSMutableArray *)timeToCompare
{

int Start1=[self getMinitesFromTime:[timeToCheck objectAtIndex:0]];
int end1=[self getMinitesFromTime:[timeToCheck objectAtIndex:1]];

int start2=[self getMinitesFromTime:[timeToCompare objectAtIndex:0]];
int end2=[self getMinitesFromTime:[timeToCompare objectAtIndex:1]];

if(start2>=end1 || end2<=Start1)
{
    NSLog(@"Clash not happeed");
    return NO;
}
else
{
    NSLog(@"clash hapens ");

    return YES;
}

}


-(int)getMinitesFromTime:(NSString *)timeINHours
{
NSInteger hrVal1 = [(timeINHours) intValue] ;

NSArray *array1 = [timeINHours  componentsSeparatedByString:@":"];
int minVal1 = [[array1 lastObject]intValue];

hrVal1 = hrVal1 *60;
minVal1 = hrVal1 + minVal1;


return minVal1;
}
于 2013-05-24T12:03:10.277 回答
1

阵列计时包含您的阵列。为了统一起见,您可能希望以 24 小时格式包含时间。

NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:@"HH:mm"];

NSDate *now = [NSDate date];

__block NSString *status = nil;

[timings enumerateObjectsUsingBlock:^(NSDictionary * dict, NSUInteger idx, BOOL *stop)
 {

     NSDate *date1 = [dateFormatter dateFromString:dict[@"time_from"]];
     NSDate *date2 = [dateFormatter dateFromString:dict[@"time_to"]];

     NSDateComponents *date1Components = [[NSCalendar currentCalendar]components:NSHourCalendarUnit|NSMinuteCalendarUnit
                                                                        fromDate:date1];

     NSDateComponents *date2Components = [[NSCalendar currentCalendar]components:NSHourCalendarUnit|NSMinuteCalendarUnit
                                                                        fromDate:date2];

     NSDateComponents *nowDateComponents = [[NSCalendar currentCalendar]components:NSHourCalendarUnit|NSMinuteCalendarUnit
                                                                              fromDate:now];

     NSInteger fromMinutes = date1Components.hour*60+date1Components.minute;
     NSInteger toMinutes = date2Components.hour*60+date2Components.minute;
     NSInteger nowMinutes   = nowDateComponents.hour*60+nowDateComponents.minute;

     if (nowMinutes>fromMinutes && nowMinutes<toMinutes) {
         status = dict[@"timing_name"];
     }

 }];

NSLog(@"%@",status);
于 2013-05-24T12:29:58.150 回答