NSMutableArray is empty after addObject

I want to add an object to NSMutableArray:

NSLog(@"Object text: %@", object.text); NSLog(@"Object: %@", object); [appdelegate.objects addObject:object]; NSLog(@"Objects array size: %i", [appdelegate.objects count]); 

This is the conclusion:

 Object text: This is the text Object: <Object: 0x6e762c0> Objects array size: 0 

How is this possible, I add an object, on the next line it is still empty. NSMutableArray not nil because it would throw an exception.

Has anyone guessed?

+7
source share
2 answers

This would not throw an exception if it was nil . You can still send the nil object if it normally replies to this message. In this case, you simply get 0. I think you are not allocating an array. Make sure you do this:

 array = [[NSMutableArray alloc] init]; 

As a hint for debugging, if you are not sure about the state of the object and want to make sure that the object really exists and is ready to use, use assert(appdelegate.objects); If the array is zero, your code will stop executing on that line. If it does not stop at this line, you know that the object exists in memory.

+10
source

Your NSMutableArray is really almost null. This will not throw an exception, because sending any message to nil in ObjC is not an operator and will behave as you see with a return value of zero or nil , etc.

Try registering to double check.

0
source

All Articles