Hi in my application I have an array which contains a group of dictionaries. Each dictionary has three key value pairs. Out of three two are start date and end date. Now I want to pass a date and have it fetch a dictionary from an array where the date will be in between start and end date. If the date is not within the range of start and end dates then have to check in another dictionary and have to fetch that dictionary. Can any one please help me to do this.
Date (DD/MM/YYYY): 13/04/2014
Start Date: 03/04/2014
End Date: 31/04/2014
Dictionaries:
NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"Scode",@"01-04-2014",@"StartDate",@"15-04-2014",@"EndDate",nil];
NSDictionary *dic1=[[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"Scode",@"16-04-2014",@"StartDate",@"25-04-2014",@"EndDate",nil];
NSDictionary *dic3=[[NSDictionary alloc]initWithObjectsAndKeys:@"2",@"Scode",@"26-04-2014",@"StartDate",@"30-04-2014",@"EndDate"nil];
Result:
NSPredicate *predicateName;
predicateName=[NSPredicate predicateWithFormat: @"StartDate <=%@ && EndDate >=%@",date,date];
NSArray *arrResult=[tempArray filteredArrayUsingPredicate:predicateName];
NSMutableArray *arrSearchResults = [[NSMutableArray alloc]initWithArray:arrResult];
If date is 1-4-2014 arrSearchResults is:
({
EndDate = "15-04-2014";
StartDate = "01-04-2014";
SCode = 0;
})
If date is 2-04-2014 arrSearchResults is:
({
EndDate = "25-04-2014";
StartDate = "16-04-2014";
SCode = 1;
})
Please check the answer:
NSMutableArray *arr=[[NSMutableArray alloc]init];
NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"Scode",@"01-04-2014",@"StartDate",@"15-04-2014",@"EndDate",nil];
NSDictionary *dic1=[[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"Scode",@"16-04-2014",@"StartDate",@"25-04-2014",@"EndDate",nil];
NSDictionary *dic2=[[NSDictionary alloc]initWithObjectsAndKeys:@"2",@"Scode",@"26-04-2014",@"StartDate",@"30-04-2014",@"EndDate",nil];
[arr addObject:dic];
[arr addObject:dic1];
[arr addObject:dic2];
NSPredicate *predicateName;
predicateName=[NSPredicate predicateWithFormat: @"StartDate <=%@ && EndDate >=%@",@"02-04-2014",@"02-04-2014"];
NSArray *arrResult=[arr filteredArrayUsingPredicate:predicateName];
NSMutableArray *arrSearchResults = [[NSMutableArray alloc]initWithArray:arrResult];
arrSearchResults will be the result.
Replace 02-04-2014 date as the your date using which you want to apply the filter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With