Tisha Anand
Tisha Anand

Reputation: 319

Adding NSMutableDictionary to NSMutableArray - iOS, Objective - C

In a ViewController (A), I have created a property as below:

@property (strong, nonatomic) NSMutableArray *someList;

In another ViewController (B), an instance of NSMutableDictionary is added to the someList property but it always gives null. Please help. In B,

A = [[A alloc] init];
[A.someList addObject:someInitializedMutableDictionary];

Upvotes: 0

Views: 237

Answers (3)

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16650

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.

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122381

You need to allocate the someList property, which is normally done in the init method of the subclass. However there is an issue in that the init method for view controllers isn't generally implemented in a subclass, and doing that kind of stuff is preferred in viewDidLoad.

Therefore I would recommend you create a method to add stuff to someList, like this:

@implementation A
...

- (void)addToSomeList:(id)object
{
    if (!_someList)
        _someList = [NSMutableArray new];
    [_someList addObject:object];
}

and this will allocate someList as soon as something needs to be added to it.

Upvotes: 1

TMob
TMob

Reputation: 1278

Does someList ever get initialized? You can't add to something that doesn't 'exist' yet.

You somewhere have to say:

self.someList = [[NSMutableArray alloc] init];

self of course only when you do this inside the ViewController A

Upvotes: 0

Related Questions