从nsdictionary获取日期并匹配它们

时间:2011-08-11 20:24:00

标签: iphone

我正在使用nsdictionary来存储日期。我正在尝试从我的测试用例中提供某些日期并从字典中获取日期。我的字典有10个日期。我希望匹配3个日期并且只获得3个日期来自我无法做到这一点。任何人都可以告诉我如何做到这一点?即使我尝试获得3个日期,我也会得到所有10个日期。然后,继承我的密码(([dd1 laterDate:startDate] | | [dd1 isEqualToDate:startDate])&&                        ([dd1 earlyDate:endDate] || [dd1 isEqualToDate:endDate]))                     {
                         if([startDate earlyDate:dd1]&& [endDate earlyDate: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]; 

我希望只有两个日期,但我从nsdictionary获得所有4个元素。我从测试用例传递我的startdate和enddate,我使用nsxmlparser从google weather api获取日期并将它们存储在nsdictionary中。我正在获取第一个日期并递增每个日期并存储它们。我的NSdictionary看起来像{     city =#;     condition =“Mostly Sunny”;     country =#;     date =“2011-08-11 05:00:00 +0000”;     “day_of_week”=周四;     高= 81;     icon =“/ig / images / weather / mostly_sunny.gif”;     低= 65;     startdate =“2011-08-11 05:00:00 +0000”;     state =#;

1 个答案:

答案 0 :(得分:0)

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;
         }
 }
  

我刚给你举了一个例子,没有通过运行来测试它。