0

I am having an issue with the addObject method of an NSMutableArrayObject. Here's the code I'm using right now:

- (void)addBirdSightingWithName:(NSString *)name location:(NSString *)location {
    BirdSighting *bird;
    NSDate *today = [NSDate date];
    bird = [[BirdSighting alloc] initWithName:name location:location date:today];
    [self.masterBirdSightingList addObject:bird];
    NSLog(@"Elements: %d", [self.masterBirdSightingList count]);
}

When this code runs, the NSLog call prints the value 0 to the console. I don't know what could be causing this.

EDIT:

I have looked deeper into the code, and I have discovered that the problem is that my BirdSightingDataController is never initialized. Now my question is: Where can I place the init for my BirdSightingDataController? In the viewDidLoad?

Thanks to everyone for the help.

3
  • You don't seem to be creating UITableViewCells anywhere, just dequeuing existent (if any). How do you know it's not getting called? Place a NSLog with something at the first line of the function and post results. Commented Mar 10, 2012 at 13:05
  • Is that a cpoy&paste error or did you ommit the : after cellForRowAtIndexPath? Since the colon is part of the method name it matters. Commented Mar 10, 2012 at 13:15
  • Take a look at the edit. My error seems to be in the model. Thanks for all your comments Commented Mar 10, 2012 at 13:23

3 Answers 3

1

Did you allocate memory to masterBirdSightingList?

self.masterBirdSightingList = [[NSMutableArray alloc] init];
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, I did alloc the NSMutableArray
and are you completely sure that the allocation call is actually made, and that it's made before you add the first element? And, have you @synthesized the get/set functions? or may be failing at the moment to write or get the value from the variable in a custom get/set method? Try putting an NSLog() when allocating masterBirdSightingList. Everything points out that it's not allocated at the moment you call [count].
You are right man. The init of the class BirdSightingDataController is never called. Any idea where can I do this? I supposed that controller was automatically called when the method was Synthesized.
Sure, no problem. @tipycalFlow was right from the beginning. Thank him accepting his answer :)
@afruizc, you should thank Gonzalo Larralde. I merely gave a pointer!
0

In almost every case where cellForRowAtIndexPath: is not called is because numberOfRowsInSection: returns 0.

Place a log there and make sure you return more than one item and you should be able to see your cells. If you need further help please post the code in your:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

2 Comments

actually the line that is returning nil is referring to the NSMutableArray. Nothing to do with the UITableView. @afruizc, remember that every message sent to nil (in this case, self.masterBirdSightingList = nil), will return nil.
The opening question changed so now my answer will not be matching the question asked. :o
0

Check your property accessor. Are you sure it's returning the correct object? Make sure the name of the property matches the instance variable or you've specified it correctly (For example: @synthesize masterBirdSightingList = _masterBirdSightingList;. If the property accessor doesn't match the iVar, it will return nil. Of course, if you're manually implementing the accessor check your code there. If you're not, you could also try manually implementing it to make sure.

To do a quick check, remove the self.masterBirdSightingList and replace it with masterBirdSightingList (assuming that's the iVar name) to access the iVar directly and see what happens.

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.