开发者

get date from an nsdictionary and match them

I am using nsdictionary to store dates.I am trying to give certain dates from my testcase and get the dates from the dictionary.Say ,my dictionary has 10 dates.I wish to match 3 dates and get only the 3 dates from the dictionary.I am unable to do that.Can anyone tell me how I can do that?I am getting all the 10 dates even I try to get the 3 dates.well,heres my codeif(([dd1 laterDate:startDate] || [dd1 isEqualToDate:startDate]) && ([dd1 earlierDate:endDate] || [dd1 isEqualToDate:endDate] ) ) {

if ([startDate earlierDate:dd1] && [endDate earlierDate:dd1])

                   {    


                        //dd==startDate;
                        NSManagedObject *allnew = Nil;
                        NSManagedObjectContext *allone=[self managedObjectContext];            
                        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Weather" inManagedObjectContext:allone];   
                        NSLog(@" The NEW ENTITY IS ALLOCATED AS entity is %@",entity);
                        WeatherXMLParser *delegate = [[WeatherXMLParser alloc] initWithCity:city state:state country:country];
                        NSXMLParser *locationParser = [[NSXMLParser alloc] initWithContentsOfURL:delegate.url];
                        [locationParser setDelegate:delegate];
                        [locationParser setShouldResolveExternalEntities:YES];
                        [locationParser parse];
                        NSFetchRequest *request = [[NSFetchRequest alloc] init];  
                        [request setEntity:entity]; 

                        predicate = [NSPredicate predicateWithFormat:
                                     @"city == %@ and state == %@ and country == %@  and date==%@ and date==%@", city, state, country,startDate,endDate];

                        [request setPredicate:predicate];
                        NSError *err;
                        NSUInteger count = [allone countForFetchRequest:request error:&err];
                        NSLog(@" minimum salary is %@",predicate);
                        // If a predicate was passed, pass it to the query
                        if(predicate !=NULL){

                            //[self deleteobject];
                        }

                        Weather *weather = (Weather *)[NSEntityDescription insertNewObjectForEntityForName:@"Weather" 
                                                                                    inManagedObjectContext:self.managedObjectContext]开发者_如何学编程;
                        weather.date = [fields objectForKey:@"date"];
                        weather.high =[fields objectForKey:@"high"];
                        weather.low = [fields objectForKey:@"low"];
                        weather.city =[fields objectForKey:@"city"];
                        weather.state =[fields objectForKey:@"state"];
                        weather.country =[fields objectForKey:@"country"];
                        NSString*icon=[fields objectForKey:@"icon"];
                        NSString *all=[icon lastPathComponent];
                        weather.condition = all;

                        [self saveContext]; 

I wish to get only 2 dates but I am getting all 4 elements from nsdictionary.I am passing my startdate and enddate from the testcase and I am getting dates from google weather api using nsxmlparser and storing them in nsdictionary.I am getting the first date and incrementing each date and storing them.My NSdictionary looks likegot { city = #; condition = "Mostly Sunny"; country = #; date = "2011-08-11 05:00:00 +0000"; "day_of_week" = Thu; high = 81; icon = "/ig/images/weather/mostly_sunny.gif"; low = 65; startdate = "2011-08-11 05:00:00 +0000"; state = #;


int count = 0;// global

 -(void)threeDateMatches(NSDate*)testCaseDate{
     for(NSDate* d1 in dateDictionary){
         if( [[dateComparisonFormatter stringFromDate:testCaseDate] isEqualToString:   [dateComparisonFormatter stringFromDate:d1]] ) {
               count = count+1;
               //save your d1 in another variable and that will give you three matches date
               //with your testCaseDate
         }
         if(count>3){
               break;
         }
 }

I have just given you an example, did not test it by running it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜