Andrew
Andrew

Reputation: 3892

NSArray count not working

So I have a UITableView, and I populate the tableview with data from a .plist. This has been working fine for me until today, when I tried to do the same and I can't get the numberOfRowsInSection, method to work. Here is my code:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     if...

     else if (segmentOptions == exchange){
         NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:section]]];
         return [array count];
     }   

   //Else
    return contentArray.count;
}

So in that code, everytime I run it, the code crashs. But, I use basiacally the same code to set the tableview text and that works fine:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *CellIdentifier = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
     }

     if...

     else if (segmentOptions == exchange) {
           NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:indexPath.section]]];
           cell.textLabel.text =  [array objectAtIndex:indexPath.row];
     }

     return cell;
}

And that code works fine, which is why I'm so confused. When I set the number of rows to return to a specific number, and run the rest of the code, everything works fine. So I'm really lost. Thands in advance

UPDATE I tried it with the proper count notation, return [array count];, and I still get the same crash and the same console output.

Upvotes: 0

Views: 2627

Answers (3)

Mustafa
Mustafa

Reputation: 20564

Break it down and debug, if console message is not being helpful. It helps sometimes, specially when it's late. i.e.

// NSArray *array = [NSArray arrayWithArray:[exchangeDictionary objectForKey:[listOfExchanges objectAtIndex:section]]];

NSLog(@"%d", section);

id objAtIndex = [listOfExchanges objectAtIndex:section];
NSLog(@"%@", listOfExchanges);
NSLog(@"%@", objAtIndex);

id objForKey = [exchangeDictionary objectForKey:objAtIndex];
NSLog(@"%@", exchangeDictionary);
NSLog(@"%@", objForKey);

Upvotes: 2

nicholjs
nicholjs

Reputation: 1762

Try this:

[array count]

count is not a property, it is a method you run on the array.

Upvotes: -3

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

First, count is not a property so you should not be using dot syntax to access it.

I would suggest changing your code so that you are not accessing the count method like a property.

Second,

Test to see if your array is nil and that it is even an array.

Third,

Post the actual complete stack trace.

Upvotes: 5

Related Questions