Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8012347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:18:15+00:00 2026-06-04T19:18:15+00:00

If I have a property, say an NSArray, that’s going to be initialized only

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:18:17+00:00Added an answer on June 4, 2026 at 7:18 pm

    This is a totally valid way of setting up your property. Apple does this sort of thing very often in their sample code as well as in their project templates. Look, for example, at the core data stack setup in a newly created core data iOS project. As @WendiKidd noted, you have to access your variable through the accessors all the time to make sure this works nicely (which is probably what you should be doing anyway).

    In particular, this is a good way of implementing a property of a class that can really only return one thing (which, from your comment, it sounds like is what you’re trying to do). If that’s your goal, here are some guidelines to follow:

    1. Declare your property as readonly
    2. Declare it in the public header if it should be publicly accessible, or in a class extension in the .m file if it should be “private”
    3. If it can/should be backed by a variable, synthesize the ivar and overwrite the getter as you’ve done
    4. If it shouldn’t/doesn’t have to be backed by a variable, declare the property as @dynamic in the implementation and overwrite the getter
    5. Only access your ivar through the accessor

    Declaring the variable as dynamic in the 4th point will signal to anyone that looks at your code that you’ve likely written a custom accessor for that property.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say, I have a partial class that contains a custom property like this public
Say I have a tableview class that lists 100 Foo objects. It has: @property
Say I have this Class @interface CustomClass : NSObject @property (nonatomic, strong) NSArray *
Say I have a class Customer which has a property FirstName . Then I
Say I have two classes and have a requirement that the primary key property
I have a property within a class, that I need to iterate through all
Say I have: @property (nonatomic,retain) NSString *foo; in some class. And I call: myclass.foo
In my NSArray, I have two types of objects, let's say objects of class
Lets say I have a property that IsRegistrationCompleted . I have 2 checkboxes in
Say I have a C# Nullable DateTime? property that needs to be consumed by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.