I tried a few things and I'm stumped by this. In a UIView object I have an NSArray object declared in my @interface, and set in - (void)awakeFromNib. The UIView is a data source for its UITableView, and to get the number of rows I'm calling the count method, which causes an ERR_BAD_ACCESS. Here is the weird part, if I do
Code:
NSLog(@"%@", [self.streamsArray count]);
I get an ERR_BAD_ACCESS, but if I call
Code:
NSLog(@"%@", self.streamsArray);
everything prints out fine. So why would it be that calling the count method doesn't work?
So why would it be that calling the count method doesn't work?
The string specifier %@ is only used for printing Objective-C objects. The count method returns an NSUInteger, which is not an object. Change the format specifier to %u, which is used for printing unsigned integers, and you should be fine.
and when it gets to the tableView:numberOfRowsInSection: method I get an ERR_BAD_ACCESS called. I added streams as a property and then called it only though self.streams, seemed to fix it. Dunno why, anyone know? I'd like to avoid such problems in the future.
The arrayWithObjects methods returns an autoreleased array. You can tell this because the method name doesn't begin with "new", "alloc", or "copy". This means you have to explicitly retain the result, or it will be deallocated whenever the current autorelease pool is drained. This is why you're getting the bad access error -- the memory has already been automatically reclaimed by the time you're trying to use it.
You could fix this by simply retaining streams, like so:
But, your solution of using a retained property is less error prone than accessing the instance variable directly, since the generated property methods will handle the memory management for you. In either case, make sure you release streams in your dealloc method.
If any of this is confusing, a good place to start would be the Cocoa Memory Management Programming Guide. BrianSlick also wrote a really nice beginner's guide to using properties, which might be helpful as well.
Added it, tried it, works. Thank you very much for that explanation. As a former PHP guy there is a lot of stuff I never had to think about before on the iPhone.