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 8409415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:54:15+00:00 2026-06-09T23:54:15+00:00

I have a property @property (strong, nonatomic, readonly) NSMutableArray* myArray; and I want to

  • 0

I have a property

@property (strong, nonatomic, readonly) NSMutableArray*     myArray;

and I want to lazily create the array in the getter

- (NSMutableArray*)myArray
{
    if(_myArray == nil)
    {
        _myArray = [NSMutableArray arrayWithCapacity:4];
    }
    return _myArray;
}

but this breaks the automatic synthesis of the iVar (`_myArray), negating some of the benefits of automatic synthesis meaning you have to do things the old way.

This is a very common pattern and it would be nice to have automatic synthesis of a lazy-create version of the getter. I guess this would take an extension to the Objective-C language and compilers, for example with an additional property attribute like this:

@property (strong, nonatomic, readonly, lazycreate) NSMutableArray*     myArray;

Any class that implements a pre-defined class method (in a similar way to object subscripting described here) such as + (ClassType*)defaultConstructor could support the lazycreate property attribute and automatic synthesis could then synthesize the getter in my example like this:

- (NSMutableArray*)myArray
{
    if(_myArray == nil)
    {
        _myArray = [NSMutableArray defaultConstructor];
    }
    return _myArray;
}

Am I right that this would require a language extension, or is there a clever way to achieve it now?

Are there any problems or pitfalls with this idea and what are they?

How do I propose this idea into the Objective-C language?

  • 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-09T23:54:16+00:00Added an answer on June 9, 2026 at 11:54 pm

    Adding a new property attribute like lazycreate requires modifying the compiler. You can ask Apple to add it by opening a new problem at http://bugreport.apple.com. The product is “Developer Tools” and the classification is “Feature (New)”.

    You could define a macro like this:

    #define synthesizeLazy(Type, Property) \
        synthesize Property = _##Property; \
        - (Type *)property { \
            if (_##Property == nil) { _##Property = [[Type alloc] init]; } \
            return _##Property; \
        }
    

    and then use it to synthesize your lazy properties like this:

    @synthesizeLazy(NSMutableArray, myArray);
    

    There’s no good way to fake it with completely automatic synthesis.

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

Sidebar

Related Questions

With ARC enabled I have a property @property (strong, nonatomic) NSMutableArray *arr; I Allocate
On my app delegate class I have a simple property @property (strong, nonatomic) LoginAppDelegate
I have a custom UITableViewController with 2 properties: @property (strong, nonatomic) IBOutlet VenueHeaderViewController *header;
I have the following in my property @property (nonatomic,readonly) NSString itemName; originally readonly ==
I have a class: @interface BasicDescription : NSObject @property (strong,nonatomic) NSString *author; @property (assign,nonatomic)
If I have a read-only string property, is it necessary to specify strong (or
I have a property declared in my .h file as @property (weak, nonatomic) UIPickerView
I have a string property bound to a textbox. On the same screen there
I have a string property that I would like to be able to force
I'm using the Validation Application Block from Microsoft. I have a string property which

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.