1

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;
   }
}
taskinoor
  • 45,586
  • 12
  • 116
  • 142
iJar
  • 147
  • 2
  • 15

2 Answers2

2

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
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
1

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;
}
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • HI I insert your code but I am still getting an error: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Month.' – iJar Apr 02 '11 at 12:04
  • In which line? There might be problems other than comparing the date. Can you run the code without the date comparing part? – taskinoor Apr 02 '11 at 12:15
  • And what is aMonth.stagesPerMonth and aStage? – taskinoor Apr 02 '11 at 12:16
  • Without the date comparing part it works perfectly with the comparing part it gives the error when compiling. On line "[aStage setValue:currentElementValue forKey:elementName];" with argument: 'Program received signal: "SIGABRT"'. It is almost like without the comparison it knows what 'elementName' is and with the comparison it doesn't recognize it. – iJar Apr 02 '11 at 12:20
  • aMonth.stagesPerMonth is an array aStage's. What it does is it has an array of array's which is provided to the Tableview. aMonth->Section, aStage-> Row. But this works all fine it is the moment that I add the comparison code when it freaks out. – iJar Apr 02 '11 at 12:26
  • 1
    Well, it is not crashing in the comparing part. Is it possible that releasing aStage and setting it to nil when the condition is true is making the crash during next call? – taskinoor Apr 02 '11 at 12:55
  • Bingo I had to place the [aStage release]; aStage = nil; outside of this if statement. Many thanks Man!!! – iJar Apr 02 '11 at 13:19