So I have an NSMutable array that I populate in viewDidLoad and then I NSLog it and it returns NULL in the console… I have looked around on stack-overflow and the apple reference and cannot find a solution to my problem.
Heres the code I used:
//Populate array
-(void)populateArray{
[TextArray addObject:@"Text1"];
[TextArray addObject:@"Text2"];
[TextArray addObject:@"Text3"];
[TextArray addObject:@"Text4"];
[TextArray addObject:@"Text5"];
NSLog(@"%@",TextArray);
NSLog(@"%@",[TextArray objectAtIndex:1]);
}
-(void)viewDidLoad
{
TextArray = [[NSMutableArray alloc] init];
[self populateArray];
}
And in the .h file
NSMutableArray *TextArray;
Thanks in advanced!
~Matt
the code you posted does not contain enough information for us to provide a specific answer. chances are, the methods are not being called in the order you expect them (e.g. populateArray is called from another point before viewDidLoad).
some assertions should help diagnose this:
lastly, if this array never changes, it’s going to be much simpler to create this in your initializer rather than lazily. such an array will not require much memory, in case that was your concern.
Update the construct takes this form:
lastly,
viewDidLoadmay be certainly be called multiple times on the same instance. the system unloads (then lazily reloads) views in low memory conditions.