iPhone dev:尝试比较当前日期和数组元素的日期
我有一个带有部分的工作 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个简单的方法是使用
NSDate
的compare
方法:A simple way is to use
NSDate
'scompare
method:您以错误的方式比较日期。试试这个。
You are comparing the dates in wrong way. Try this.