简体   繁体   中英

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.

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.

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

// 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);

Try this:

[array count]

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM