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?