Lazy Instantiation not working iOS

138 views Asked by At

I've declared a property in the .h file called cellTitles. In my .m file, I have a method as follows:

-(NSArray *)cellTitles
{
    if(!_cellTitles){
        _cellTitles = [[NSArray alloc] initWithObjects:@"several strings", nil];
        NSLog(@"Home Array Created");
    }
    return _cellTitles;
}

But the array is not created when I refer to _cellTitles or self.cellTitles. I have several NSLogs that all say the array has 0 objects. Do I need more than this. Some answers have said I need to synthesize, but as I understand it, that is no longer necessary.

1

There are 1 answers

3
karlofk On

Make sure your property is assigned strongly.

@property (nonatomic, strong) NSArray *cellTitles;