59

I have declared an NSMutableArray *categories in my view controller .h file, and declared a property for it.

In the parser:foundCharacters: method of the NSXMLParser delegate in my .m file, I have this code:

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string  
{  
    if (elementFound)  
    {  
        element = string;  
        [self.categories addObject:element];  
    }  
}

But when I hover over the [self.categories addObject:element] line after stepping into it in debug mode, XCode tells me the size is 0x0, 0 objects. There are 3 elements in my XML file so 3 items should be in the array.

I'm missing something really obvious and I can't figure out what.

John Rudy
  • 37,282
  • 14
  • 64
  • 100
joec
  • 3,533
  • 10
  • 60
  • 89
  • 3
    For your future posting: the way to format code is to select the code block, and indent everything 4 spaces by using the "code" formatter button at the top of the post editor. – Jonathan Feinberg Dec 01 '09 at 15:55
  • sorry - i copied and pasted from another forum, guess the formatting didnt hold up to much! thanks – joec Dec 01 '09 at 16:20

2 Answers2

150

The "0x0" part is a memory address. Specifically, "nil", which means your mutable array doesn't exist at the time this is being called. Try creating it in your -init method:

categories = [[NSMutableArray alloc] init];

Don't forget to release it in your -dealloc.

Joshua Nozzi
  • 60,946
  • 14
  • 140
  • 135
  • 12
    I don't know why I keep forgetting to do this. Thanks. – James May 08 '12 at 13:57
  • 17
    (2012 Update: If you're using ARC, the release/dealloc part can be ignored.) – Joshua Nozzi May 08 '12 at 14:01
  • 4
    Thank you! It's easy to keep forgetting that messages to nil object references don't fail in Objective-C. – JasonD Jun 01 '12 at 06:52
  • Come from Swift, I keep forgetting to `new` NSMutable array too:) In Swift we usually initialize the array when declare it or give it an optional mark. While in Objective-C, apparently it works differently. And compiler will not warn yout about that in OC. – Zhou Haibo Sep 10 '21 at 05:55
8

Initialize an empty array using

categories = [NSMutableArray array];

The array class method are autoreleased so no need to release.

iamVishal16
  • 1,780
  • 18
  • 40
  • Vishu - Your solution is working, but I think Joshua Nozzi's solution is better since it needs less memory. – Itai Spector Sep 17 '15 at 13:38
  • 1
    This is functionally equivalent to my answer five and a half years ealier, just using the convenience method instead of alloc/init. There is no difference in memory usage and the result is exactly the same. – Joshua Nozzi Apr 25 '17 at 11:41
  • @JoshuaNozzi The array class method by itself produces an autoreleased array, meaning you don't have to (and should not) release it manually. – iamVishal16 Jul 23 '18 at 05:51
  • I’m aware of that. My point is that you added a functionally-equivalent answer six years later, well after manual reference counting was no longer a concern to begin with due to ARC. Why not answer a question that hasn’t already been answered or answer one with a demonstrably better answer? – Joshua Nozzi Jul 23 '18 at 15:32