I’m sure I’m doing something silly, but this is driving me crazy.
I’m trying to loop through database results, create objects from those results, and add the objects to an NSMutableArray. I’ve verified via NSLog calls that the data is being correctly read from the database and copied to the object, but the count for the NSMutableArray always returns 0.
Here’s the essence of the code:
while ([rs next]) { Kana *htemp = [Kana alloc]; htemp.content = [rs stringForColumn:@'hiragana']; [hiragana addObject:htemp]; } NSLog(@'Hiragana contains %d objects', [hiragana count]);
Kana is derived from NSObject, and hiragana is an instance of NSMutableArray.
I’m sure this is a rookie mistake, and I hope someone can set me straight. TIA! 🙂
My guess, judging from the code you posted, is that you probably aren’t allocating your array properly. When creating objects, you need to initialize them as well. Therefore, this:
Should be:
All objects need to be initialized this way. Thus, if I’m correct and you haven’t initialized your array, then your creation needs to go from this:
to this:
For optimization reasons, you should probably also specify an initial capacity as well if you have any idea how many objects you might hold: