iPhone dev:尝试比较当前日期和数组元素的日期

发布于 2024-10-29 05:40:52 字数 1195 浏览 6 评论 0原文

我有一个带有部分的工作 UITable。 UITable 从外部 XML 文件获取数据,这也很好用。但我想要的是 XMLparser 排除日期早于今天的行。我想在我的文件向数组添加一个元素时执行此操作。但是当我添加此代码时出现错误。 请帮我解决这个问题!

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
   NSDate* date = [NSDate date];
   NSDateFormatter* nsformatter = [[[NSDateFormatter alloc] init] autorelease];
   [nsformatter setDateFormat:@"yyyy-MM-dd"];
   NSDate* stageDate = [XMLParser dateFromString:aStage.end];

   if([elementName isEqualToString:@"Stages"])
   {
       return;
   }
   if([elementName isEqualToString:@"Month"])
   {
   [appDelegate.stages addObject:aMonth];
       [aMonth release];
   aMonth = nil;
   }
   if([elementName isEqualToString:@"Stage"])
   {
       /* THIS IS THE PART I ADDED BUT GIVES THE ERROR
       if(stageDate < date)
       {
       */
           [aMonth.stagesPerMonth addObject:aStage];
           [aStage release];
           aStage = nil;
       /*
       }
       */
   }
   else
   {
       [aStage setValue:currentElementValue forKey:elementName];
       [currentElementValue release];
       currentElementValue = nil;
   }
}

I have a working UITable with sections. The UITable gets its data from an external XML file this also works good. But what I want is that the XMLparser excludes the rows where the date is older then today. I thought to do this at the moment where my file adds an element to my array. But when I add this code I get an error.
Please help me out on this one!

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
   NSDate* date = [NSDate date];
   NSDateFormatter* nsformatter = [[[NSDateFormatter alloc] init] autorelease];
   [nsformatter setDateFormat:@"yyyy-MM-dd"];
   NSDate* stageDate = [XMLParser dateFromString:aStage.end];

   if([elementName isEqualToString:@"Stages"])
   {
       return;
   }
   if([elementName isEqualToString:@"Month"])
   {
   [appDelegate.stages addObject:aMonth];
       [aMonth release];
   aMonth = nil;
   }
   if([elementName isEqualToString:@"Stage"])
   {
       /* THIS IS THE PART I ADDED BUT GIVES THE ERROR
       if(stageDate < date)
       {
       */
           [aMonth.stagesPerMonth addObject:aStage];
           [aStage release];
           aStage = nil;
       /*
       }
       */
   }
   else
   {
       [aStage setValue:currentElementValue forKey:elementName];
       [currentElementValue release];
       currentElementValue = nil;
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2024-11-05 05:40:52

一个简单的方法是使用 NSDatecompare 方法:

NSComparisonResult result = [stageDate compare:aDate];  
if (result == NSOrderedAscending)     
    // stageDate is in the future
else if (result == NSOrderedDescending)     
    // stageDate is in the past
else     
    // Both dates are the same

A simple way is to use NSDate's compare method:

NSComparisonResult result = [stageDate compare:aDate];  
if (result == NSOrderedAscending)     
    // stageDate is in the future
else if (result == NSOrderedDescending)     
    // stageDate is in the past
else     
    // Both dates are the same
转瞬即逝 2024-11-05 05:40:52

您以错误的方式比较日期。试试这个。

NSTimeInterval interval = [date timeIntervalSinceDate:stageDate];
if (interval > 0) {
    [aMonth.stagesPerMonth addObject:aStage];
    [aStage release];
    aStage = nil;
}

You are comparing the dates in wrong way. Try this.

NSTimeInterval interval = [date timeIntervalSinceDate:stageDate];
if (interval > 0) {
    [aMonth.stagesPerMonth addObject:aStage];
    [aStage release];
    aStage = nil;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文