Possible Duplicate:
NSMutableArray addObject not working
I'm making an iPhone app, and so far, I'm receiving data from my server, creating objects using that data and filling an array with those objects.
My data is in XML format, and its saved into a string, which is transformed into a NSData
object, like this:
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://my.URL/data.php"]];
[myRequest setHTTPMethod:@"POST"];
NSURLResponse *response;
NSError *error = NULL;
NSData *myReturn = [NSURLConnection sendSynchronousRequest:myRequest returningResponse:&response error:&error];
NSString *returnString = [[NSString alloc] initWithData:myReturn encoding:NSASCIIStringEncoding];
NSData *tempData = [return dataUsingEncoding:NSUTF8StringEncoding];
After that, I do the standard objective-C XML event parsing, but I don't create anything until the following method:
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqual:@"id"])
{
hailID = currentValue;
return;
}
if ([elementName isEqual:@"timestamp"])
{
timeStamp = currentValue;
return;
}
if ([elementName isEqual: @"lat"])
{
hailLat = currentValue;
return;
}
if ([elementName isEqual:@"lng"])
{
hailLng = currentValue;
return;
}
if ([elementName isEqual:@"address"])
{
address = currentValue;
return;
}
if ([elementName isEqual:@"serviceType"])
{
serviceType = currentValue;
return;
}
if ([elementName isEqual: @"hail"])
{
Hail *newHail = [[Hail alloc]init];
newHail.hailID = hailID;
newHail.hailLat = hailLat;
newHail.hailLng = hailLng;
newHail.address = address;
newHail.timeStamp = timeStamp;
newHail.serviceType = serviceType;
[hails addObject:newHail];
NSLog(@"%u", [hails count]);
return;
}
}
hails
is declared in the header file, and it's just a NSMutableArray
with a capacity of 10000 items. The Hail
object is a separate class. The NSLog
always returns 0, even though I know that the XML code itself is valid, and the hail
object exists.
Any ideas on why the hails
array is always zero?
Edit: Sorry, I forgot to mention that the hails
array is initialized in the -(void)viewDidLoad
method as this:
hails = [[NSMutableArray alloc]initWithCapacity:10000];