0

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];
1
  • You have to initialize your someList property otherwise it will always remain null Commented Nov 13, 2015 at 11:56

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

There are KVC method conventions for this …
@AminNegm-Awad Please elaborate.
@AminNegm-Awad Ah yes; using that would make more sense.
Yes, esp. because you get benefits as -mutableArrayValueForKey:@"items"
0

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

1 Comment

Allocating properties from outside is a poor pattern to promote.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.