0

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];
Community
  • 1
  • 1
docaholic
  • 613
  • 9
  • 29
  • Is the NSArray within the Hail class initialized when the 'Hail' object itself is initialized? Post the initmethod. – Martol1ni Oct 18 '12 at 18:07
  • Try `NSLog(@"%@", hails)` – I wouldn't be surprised if your array is actually `nil`. – omz Oct 18 '12 at 18:07
  • This could be happening if `hails` is a weak property/reference and it is the only reference to the `NSMutableArray`. You might try creating a private strong property instead of using an ivar if this is the case. – Beltalowda Oct 18 '12 at 18:12
  • Where is line init'ing `hails` ? Are you sure that it is init'ed when you are assigning `newHail` to it? I'd suggest a breakpoint there and step through the code. – Michael Kernahan Oct 18 '12 at 18:15
  • @MichaelKernahan Good point. He mentioned above that it is being initialized with a capacity of 10,000 so I assumed he had checkded that the init had worked. – Beltalowda Oct 18 '12 at 18:16
  • At the end of your sequence there also NSLog `hails` itself, to see if it exists. You may be initializing a different copy of it, or initializing it after you need it. – Hot Licks Oct 18 '12 at 18:32
  • 1
    You need to prove to us that `hails` is really there in that last `if` statement. There are a hundred different screw-ups that can cause it to not be there, even if you're "sure" that it got created. – Hot Licks Oct 18 '12 at 19:40
  • hmmm okay I'll be sure to check that out then; it maybe that you're right – docaholic Oct 18 '12 at 19:41
  • okay thanks to everybody's suggestions! I managed to fix it, it turns out that the hails object referenced in my `if` statement was not the same `hails` object that I was expecting the newHail to be added to. Thanks to everybody for your suggestions! – docaholic Oct 18 '12 at 19:43

1 Answers1

2

Your hail is always nil because you never allocated + initialized it. You don't even mention its initialization into your code

Mr Bonjour
  • 3,330
  • 2
  • 23
  • 46