Note that there are some explanatory texts on larger screens.

plurals
  1. POArray count is 1 after XML parsing iPhone
    text
    copied!<p>I'm parsing flight times in my iPhone app. </p> <p>Here's a snippet from the XML</p> <pre><code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt; &lt;airport xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; &lt;flights lastUpdate="2011-11-07T10:49:43"&gt; &lt;flight uniqueID="2131560"&gt; &lt;airline&gt;DY&lt;/airline&gt; &lt;flight_id&gt;DY1052&lt;/flight_id&gt; &lt;dom_int&gt;S&lt;/dom_int&gt; &lt;schedule_time&gt;2011-11-04T06:00:00&lt;/schedule_time&gt; &lt;arr_dep&gt;D&lt;/arr_dep&gt; &lt;airport&gt;SVG&lt;/airport&gt; &lt;via_airport&gt;CPH&lt;/via_airport&gt; &lt;check_in&gt;C&lt;/check_in&gt; &lt;/flight&gt; &lt;flight uniqueID="2136807"&gt; &lt;airline&gt;SK&lt;/airline&gt; &lt;flight_id&gt;SK308&lt;/flight_id&gt; &lt;dom_int&gt;D&lt;/dom_int&gt; &lt;schedule_time&gt;2011-11-07T07:15:00&lt;/schedule_time&gt; &lt;arr_dep&gt;D&lt;/arr_dep&gt; &lt;airport&gt;SVG&lt;/airport&gt; &lt;via_airport&gt;SVG&lt;/via_airport&gt; &lt;check_in&gt;EF&lt;/check_in&gt; &lt;/flight&gt; ... </code></pre> <p>I store each flight in a Flight object which looks like this:</p> <pre><code>@interface Flights : NSObject{ NSInteger flightUniqueID; NSString *airline; NSString *flight_id; NSString *dom_int; NSDate *schedule_time; NSString *arr_dep; NSString *airport; NSString *check_in; NSString *status; NSString *via_airport; NSString *gate; NSString *delayed; } @property(nonatomic, readwrite)NSInteger flightUniqueID; @property(nonatomic, retain)NSString *airline; @property(nonatomic, retain)NSString *flight_id; @property(nonatomic, retain)NSString *dom_int; @property(nonatomic, retain)NSDate *schedule_time; @property(nonatomic, retain)NSString *arr_dep; @property(nonatomic, retain)NSString *airport; @property(nonatomic, retain)NSString *check_in; @property(nonatomic, retain)NSString *status; @property(nonatomic, retain)NSString *via_airport; @property(nonatomic, retain)NSString *gate; @property(nonatomic, retain)NSString *delayed; </code></pre> <p>I use an NSXMLParser to parse the XML</p> <pre><code>- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{ if ([elementName isEqualToString:@"airport"]) { allFlights = [[NSMutableArray alloc]init]; } else if([elementName isEqualToString:@"flight"]){ flight = [[Flights alloc]init]; flight.flightUniqueID = [[attributeDict objectForKey:@"uniqueID"]integerValue]; } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { if(!currentElementValue) currentElementValue = [[NSMutableString alloc] initWithString:string]; else [currentElementValue appendString:string]; } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([elementName isEqualToString:@"flights"]) { return; } if ([elementName isEqualToString:@"flight"]) { NSLog(@"Adding object: %@" , flight); [allFlights addObject:flight]; [flight release]; flight = nil; }else [flight setValue:currentElementValue forKey:elementName]; [currentElementValue release]; currentElementValue = nil; } </code></pre> <p>When i NSLog "flight", I get this output:</p> <pre><code>Adding object: &lt;Flights: 0xsomenumber&gt; ... Adding object: &lt;Flights: 0x7064360&gt; </code></pre> <p>which goes on for however many flights there are in the xml.</p> <p>However, the array I add each object to only stores the last flight object. If I try to NSLog the array after the parsing is done, it prints out the last object, and nothing else.</p> <p>Obviously I need to have all the flights stored in the array, so I can show all of them in my tableview after the parsing is done. </p> <p>How can I achieve this? Am I missing something?</p> <p>I tried to parse the XML without releasing the flight object, but that didn't work either.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload