문제

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.

다른 팁

What you need to do here:

1) Extract startDate and endDate from the array.

2) Format the two dates with a date formatter

3) Now, compare the two dates with the current date as follows,

According to Apple documentation of NSDate compare:

 if (([currentDate compare:startDate] == NSOrderedDescending) && ([currentDate compare:endDate] == NSOrderedAscending))
{
//Date lies in between two dates
}

Hope it helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top