1

I have declared a property NSMutableArray in the header file. Then I alloc, init it in the viewDidLoad method. But when I try to add an object to the array in a different method it keeps returning (null). Am I doing some obvious mistake? Please ask if you want to see some more code.

.h

@property (strong, nonatomic) NSMutableArray *myList;

.m

- (void)viewDidLoad
{
 self.dataController = [[DataController alloc]init];
 self.myList = [[NSMutableArray alloc] init];
 [super viewDidLoad];
}

[...]

NSDictionary *myObject = [self.dataController.objectList objectAtIndex:r];
[[cell textLabel]setText:[myObject objectForKey:@"title"]];
[self.myList addObject:myObject]; 
NSLog(@"myList %@",self.myList);
NSLog(@"myObject %@",myObject);

The output prints myObject but self.myList keeps returning (null). Appreciate all help!

Edit: Fixed, thank you for your answers!

asked Dec 11, 2012 at 0:21
3
  • 3
    Are you sure the view has already loaded by the time you try to access the list? Maybe you should be initializing those properties in init instead. Commented Dec 11, 2012 at 0:25
  • Where in the app is the code where you create myObject, add it to myList, and do the logs? Commented Dec 11, 2012 at 0:32
  • @KevinBallard The code in the third snippet is in tableView:cellForRowAtIndexPath so I guess the problem is that this method loads before viewDidLoad. However the log in viewDidLoad returns before the log in tableView:cellForRowAtIndexPath does. I tried to move the code from viewDidLoad to (id)init but never got it to work. Commented Dec 11, 2012 at 16:08

3 Answers 3

1

Not sure where you are using this array. If you have to use this array before viewDidLoad is called, you can do it as,

NSDictionary *myObject = [self.dataController.objectList objectAtIndex:r];
[[cell textLabel]setText:[myObject objectForKey:@"title"]];
if (!self.myList)
 self.myList = [[NSMutableArray alloc] init];//for the first time, this will initialize
[self.myList addObject:myObject]; 

Since you are using [cell textLabel] I am assuming that you are doing this in one of the table view delegates. In that case check if you are setting self.myList = nil; any where in the class.

answered Dec 11, 2012 at 0:52

5 Comments

it is used later than viewDidLoad -- else his code wouldnt print myObject. Or would it?
@Daij-Djan, Cant predict that from myObject. It could be that he is setting dataController from previous class itself and initialization part for dataController is not called while executing this. But I think you are correct due to the usage of [cell textLabel] which should be done in cellForRowAtIndexpath. That should have been called after viewDidLoad. But in any case he has to add more info to his question.
My thoughts were that he simply initialized myObject in init as well, but didn't realize / didn't mention it.
After a lot of debugging I finally found a little piece of code that set the list to nil. Now it works. Thanks for your help!
@Emil, Glad to help. :) That's what I thought. :)
0

In your posted code i see no error. Set a breakpoint and look if the code where you init the array is called first.

answered Dec 11, 2012 at 0:28

Comments

0

I bet that viewDidLoad hasn't been called yet when the NSLogs are execute.

To ensure that the array has been initialized, try putting the initialization in your init method.

answered Dec 11, 2012 at 0:29

1 Comment

I don't have any init method yet. When I try to add one it doesn't execute.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.