views:

47

answers:

3

Hi,

I'm trying to use a global array in the appdelegate as a quick fix for a demo I'm building. The relevant code for this

.h file has the declaration

{
NSMutableArray *trnEntered;
}

@property (nonatomic, retain) NSMutableArray *trnEntered

.m file has the following code -

trnEntered = [[NSMutableArray alloc] init];
NSLog(@"%@",[trnEntered count]); // prints null.
[trnEntered addObject:@"1"];
NSLog(@"%@",[trnEntered count]); // exec bad access.

Not sure where I'm going wrong here. Looks fairly straight forward.

Thanks for the help in advance,
Teja.

A: 

The first NSLog should not print 'null', you have a problem with the way you init.Try this instead.

trnEntered = [[NSMutableArray alloc] initWithCapacity:1];
Fabien
+3  A: 

There seems to be a problem with your code:

NSLog(@"%@",[trnEntered count]); // prints null.
[trnEntered addObject:@"1"]; 
NSLog(@"%@",[trnEntered count]); // exec bad access.

Both calls to NSLog are trying to print an NSUInteger as an Objective-C object. This is going to cause a problem. You should use NSLog(@"%d", [trnEntered count]);

I would suggest that you read more on format specifiers, there is a lot of useful information.

Lyndsey Ferguson
Agh, I knew it was something like this, I can't believe I wasted 2 hours on this. thanks!
Tejaswi Yerukalapudi
You're very welcome. I too have spent too much time on easy problems :)
Lyndsey Ferguson
A: 
trnEntered = [[NSMutableArray alloc] init];
NSLog(@"%@",[trnEntered count]); // prints null.
[trnEntered addObject:@"1"];
NSLog(@"%@",[trnEntered count]); // exec bad access.

The reason is because you're using the formatter string wrong. You're trying to use an integer as a string pointer, which will of course lead to a segmentation fault (or EXC_BAD_ACCESS in apple terms). Use %d to print an integer, which is what [trnEntered count] returns.

futureelite7