If I have a property, say an NSArray, that's going to be initialized only once for each instance of my class, is there anything wrong with this:
(in the interface)
@property(strong, nonatomic)NSArray *bubbleArr;
(in the implementation)
-(NSArray*)bubbleArr
{
if(!bubbleArr)
{
NSMutableArray *tempBubbArr = [[NSMutableArray alloc] init];
// get filepath for first speech bubble image for page
NSString *speechBubbleImgPath = [[NSBundle mainBundle] pathForResource:
[NSString stringWithFormat:@"speech_%i_0", pageIndex]
ofType:@"png"];
for(int i = 1; speechBubbleImgPath; i++)
{
UIImage *speechBubbleImg = [[UIImage alloc] initWithContentsOfFile:speechBubbleImgPath];
UIImageView *speechBubbleImgView = [[UIImageView alloc] initWithImage:speechBubbleImg];
[tempBubbArr addObject:speechBubbleImgView];
speechBubbleImg = nil;
speechBubbleImgView = nil;
speechBubbleImgPath = nil;
speechBubbleImgPath = [[NSBundle mainBundle] pathForResource:
[NSString stringWithFormat:@"speech_%i_%i", pageIndex, i]
ofType:@"png"];
}
bubbleArr = [[NSArray alloc] initWithArray:tempBubbArr];
tempBubbArr = nil;
}
return bubbleArr;
}
I've never used custom accessor methods, but this seems like a clean way to set it up, so I don't have to set up each property in my viewDidLoad
or elsewhere, and don't have to worry about it being nil
. I don't recall ever actually come across code that does this. Is this the recommended way to do it? Also, I'll always want to use self.bubbleArr
to make sure this method is called, right?