You need to create an instance of NSMutableArray
and assign it to the property.
Since the object with the property is a UIViewController
created in a storyboard, you can do it in a few different places. You can override initWithCoder:
, or awakeFromNib
, or viewDidLoad
.
If you override initWithCoder:
, it is imperative that you call the super
method.
If you do it in viewDidLoad
, the array won't be created until the view is loaded, which doesn't have to happen right away.
I recommend doing it in awakeFromNib
:
@synthesize myArray = _myArray;
- (void)awakeFromNib {
_myArray = [[NSMutableArray alloc] init];
}
Another option is to just create the array lazily by overriding the getter method of the property:
@synthesize myArray = _myArray;
- (NSMutableArray *)myArray {
if (!_myArray)
_myArray = [[NSMutableArray alloc] init];
return _myArray;
}
If you do this, it is very important that you always access the array using the getter method (self.myArray
or [self myArray]
) and never by accessing the instance variable (_myArray
) directly.