You are currently viewing the BOOK: Beginning iPad Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .

I'm totally baffled. Does any one have any idea how this can happen? In the many languages I've worked with, when you add an object to an array, the array ACTUALLY CONTAINS THAT OBJECT. Object-C doesn't seem to work that way. Ok-- that felt better. I'm doing Chapter 3 UsingViews2. Most of the code from below can be found on pp. 59-61. I'm trying to do things in a more elegant way-- I hate just copying stuff from a book.

BTW, it behaves the same way even it I don't declare it as a property.I'm baffled. I never initialize imageArray anywhere, as in:
NSMutableArray *array = [NSMutableArray initWithCapacity:16];

However, I never get a runtime error when I try to add objects to it or query its size, which is always 0. I've worked with Obj-C before and I've always had to initialize object before I used them or I would get an error. I'm not getting what has changed in this SDK.

What baffles me is that the imageArray object never complains about being sent the 'addObject' and 'count' messages. My previous experience with XCode tells me that I should get some sort of error sending a specific message to an uninitialized object. Anyway, thanks for you help.

Since imageArray is not allocated it is set to nil. Sending messages to nil is perfectly valid in Objective C.

Insert

Code:

NSLog(@"imageArray is at %p",imageArray);

and you will see it points to 0x0 without an alloc.
Don't follow your response, about doesn't seem to do anything. You can't -initWithCapacity until you allocate first.

Code:

imageArray = [NSMutableArray arrayWithCapacity:16];

will allocate and initialize the array. Calling

Code:

[imageArray initWithCapacity:32];

without an alloc first does not work. It must be used in the nested
imageArray = [[NSMutableArray alloc] initWithCapacity:32];
Attempting to call it separately throws an exception, and causes a crash. The -initWithCapacity method is not and cannot be called directly by an instance of NSMutableArray. During the allocation and initialization process it is called by one of the class cluster's abstract private classes, whose public face is the NSMutableArray.