这是我在这里的示例代码我计算了两个条目的休息时间。如果我添加另一个条目意味着它覆盖了前一个休息时间..我不知道我在哪里做错了
This is my first sample i got this for 2 entries
This is my second sample i got this for 3 entries
这是我的示例代码
NSMutableArray *arrData = [[data objectForKey:@"data"]mutableCopy];
NSLog(@"%@",arrData);
NSLog display this data before adding 3entry ..
{
date = "2016-01-20";
"end_time" = "11:10:00";
"function_code" = RCV;
"operator_id" = JOHN;
"start_time" = "11:00:00";
"total_time" = 10;
"total_units" = 19;
},
{
date = "2016-01-20";
"end_time" = "12:25:00";
"function_code" = PIK;
"operator_id" = JOHN;
"start_time" = "12:15:00";
"total_time" = 10;
"total_units" = 26;
})
NSMutableDictionary *thirdEntry = [NSMutableDictionary dictionary];
[thirdEntry setObject:@"02:00:00" forKey:@"end_time"];
[thirdEntry setObject:@"PUL" forKey:@"function_code"];
[thirdEntry setObject:@"01:25:00" forKey:@"start_time"];
[thirdEntry setObject:@"45" forKey:@"total_units"];
[thirdEntry setObject:@"20" forKey:@"total_time"];
[arrData addObject:thirdEntry];
NSLog(@"%@",arrData);
//dictionary
NSMutableDictionary *dictData =[NSMutableDictionary dictionary];
NSMutableDictionary *dictData1=[NSMutableDictionary dictionary];
NSMutableArray *arrayProgressDate =[NSMutableArray array];
//storing the breaktime values
NSMutableDictionary *dictValues =[NSMutableDictionary dictionary];
for (int i =0; i<arrData.count; i++) {
dictData =arrData[i];
[arrayProgressDate addObject:dictData];
NSString *strendTime1 =[dictData objectForKey:@"end_time"];
NSLog(@"%@",strendTime1);
[dictValues setObject:strendTime1 forKey:@"end_time_value"];
if ((i + 1) < arrData.count) {
dictData1=arrData[i+1];
NSString *strStartTimeNext = [dictData1 objectForKey:@"start_time"];
NSLog(@"%@",strStartTimeNext
);
//[arrayProgressDate addObject:strStartTimeNext];
[dictValues setObject:strStartTimeNext forKey:@"start_time_value"];
//calculating break time
NSDate *endTimeDate = [[DateHelper sharedHelper ] dateFromString:strendTime1 withFormat:@"HH:mm:ss"];
NSDate *startTimeDate = [[DateHelper sharedHelper]dateFromString:strStartTimeNext withFormat:@"HH:mm:ss"];
NSTimeInterval timeElapsedInSeconds = [endTimeDate timeIntervalSinceDate:startTimeDate];
double hours = timeElapsedInSeconds / 3600.0;
NSLog(@"%f",hours);
int breakTimeInMinutes = timeElapsedInSeconds/60;
breakTimeInMinutes =ABS(breakTimeInMinutes);
NSString *newStr =[NSString stringWithFormat:@"%i",breakTimeInMinutes];
NSLog(@"%@",newStr);
[dictValues setObject:newStr forKey:@"break_time"];
[arrayProgressDate addObject:dictValues];
}
NSLog(@"%@",dictData);
}
NSLog(@"%@",arrayProgressDate);
请告诉我我在这方面做错了什么,我想计算休息时间。
答案 0 :(得分:0)
此行NSMutableDictionary * dictValues = [NSMutableDictionary dictionary];应该在里面循环。
在找到休息时间时,您还必须考虑日期。否则你会得到错误的价值。
NSMutableDictionary *thirdEntry = [NSMutableDictionary dictionary];
[thirdEntry setObject:@"02:00:00" forKey:@"end_time"];
[thirdEntry setObject:@"PUL" forKey:@"function_code"];
[thirdEntry setObject:@"01:25:00" forKey:@"start_time"];
[thirdEntry setObject:@"45" forKey:@"total_units"];
[thirdEntry setObject:@"20" forKey:@"total_time"];
[arrData addObject:thirdEntry];
NSLog(@"%@",arrData);
//dictionary
NSMutableDictionary *dictData =[NSMutableDictionary dictionary];
NSMutableDictionary *dictData1=[NSMutableDictionary dictionary];
NSMutableArray *arrayProgressDate =[NSMutableArray array];
for (int i =0; i<arrData.count; i++) {
dictData =arrData[i];
[arrayProgressDate addObject:dictData];
NSString *strendTime1 =[dictData objectForKey:@"end_time"];
NSLog(@"%@",strendTime1);
//storing the breaktime values
NSMutableDictionary *dictValues =[NSMutableDictionary dictionary];
[dictValues setObject:strendTime1 forKey:@"end_time_value"];
if ((i + 1) < arrData.count) {
dictData1=arrData[i+1];
NSString *strStartTimeNext = [dictData1 objectForKey:@"start_time"];
NSLog(@"%@",strStartTimeNext
);
//[arrayProgressDate addObject:strStartTimeNext];
[dictValues setObject:strStartTimeNext forKey:@"start_time_value"];
//calculating break time
NSDate *endTimeDate = [[DateHelper sharedHelper ] dateFromString:strendTime1 withFormat:@"HH:mm:ss"];
NSDate *startTimeDate = [[DateHelper sharedHelper]dateFromString:strStartTimeNext withFormat:@"HH:mm:ss"];
NSTimeInterval timeElapsedInSeconds = [endTimeDate timeIntervalSinceDate:startTimeDate];
double hours = timeElapsedInSeconds / 3600.0;
NSLog(@"%f",hours);
int breakTimeInMinutes = timeElapsedInSeconds/60;
breakTimeInMinutes =ABS(breakTimeInMinutes);
NSString *newStr =[NSString stringWithFormat:@"%i",breakTimeInMinutes];
NSLog(@"%@",newStr);
[dictValues setObject:newStr forKey:@"break_time"];
[arrayProgressDate addObject:dictValues];
}
NSLog(@"%@",dictData);
}
NSLog(@"%@",arrayProgressDate);