The basic problem of your approach is that a collection property shouldn't be mutable. There are several problem with it. So, first let us change this. Additionally you have to add mutating methods (mutators), instead.
@interface MyClass
@property (readonly, nonatomic) NSArray *items; // Better naming
-(void)insertObject:(Item*)item inItemsAtIndex:(NSUInteger)index; // See below
@end
Doing so there are two "problems":
- How can one change the property?
- Isn't the ivar immutable, too?
Mutable ivar
To get a mutable ivar, just declare it in the implementation:
@implementation MyClass
{
NSMutableArray _items;
}
This ivar will be used by synthesized properties, because it has the right name and a legal type (subclass of property).
You can initialize that ivar early in the objects life cycle, i. e. -init, -viedDidLoad, $whatever. Or you can do it lazily in the accessor methods(example in trojanfoe's answer).
Additionally you can have a setter internally, if you add a class continuation in the .m-File switching the property to read/write.
If you have a setter, you should explicitly define that having a mutable copy:
-(void)setItems:(NSArray*)items // Arg type from property
{
_items = [items mutableCopy];
}
Changing
There are several KVC methods you should implement. You can find a list here. I. e.:
-(void)insertObject:(Item*)item inItemsAtIndex:(NSUInteger)index
{
[_items insertObject:item atIndex:index];
}
Usually it is simply sending one appropriate message to the collection.
someListproperty otherwise it will always remain null