我使用nsdictionary来存储日期。我正试图从我的测试案例中给出某些日期,并从字典中获取日期。比如说,我的字典有10个日期。我希望匹配3个日期,并且只从字典中得到这3个日期。我不能那样做。有人能告诉我怎么做吗?我得到了所有的10次约会,即使我试着得到3次约会。这里是我的codeif(([dd1 isEqualToDate:startDate] || [dd1 isEqualToDate:startDate]) &&([dd1 isEqualToDate:endDate] | [dd1 isEqualToDate:endDate])){
if ([startDate: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];
我希望只得到2个日期,但我从nsdictionary得到所有4个元素。我正在从测试用例传递我的开始日期和结束日期,我正在使用nsxmlparser从谷歌天气api获取日期并将它们存储在nsdictionary中。我正在获取第一个日期并增加每个日期并存储它们。我的NSdictionary看起来像got {城市= #;condition = "基本晴天";国家= #;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 = #;
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;
}
}
我刚刚给了你一个例子,没有通过运行来测试它。