user751162
user751162

Reputation: 23

Find number of objects in NSMutableArray

I have a problem with counting numbers of object in nsmutablearray. I have this code:

  - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView  accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath 
 {    

NSUInteger numObjects = [selected count];// selected is nsmutablearray

for ( int i = 0; i<numObjects; i++) 
{
    NSLog(@"%@ == %@ , nr.object =%@",[AnotherArray objectAtIndex:indexPath.row], [selektuara objectAtIndex:i], numObjects); // here stops the by reporting this: Program recived signal: "EXC_BAD_ACCESS"


    if ([selected objectAtIndex:i] == [AnotherArray objectAtIndex:indexPath.row]) 
    {
        NSLog(@"%@",[selected objectAtIndex:i]);
        return UITableViewCellAccessoryCheckmark;
    }
    else
    {
        return UITableViewCellAccessoryNone;
    }
  }
 }

When i remove NSLog for numbers of object, it counts only one object and compare only that. So has anyone else another solution how to get this number? Thanks.

Upvotes: 0

Views: 836

Answers (2)

Ole Begemann
Ole Begemann

Reputation: 135588

You must not try to output an unsigned integer with the %@ format specifier. Use %u instead.

Upvotes: 1

Nekto
Nekto

Reputation: 17877

Error in using NSLog:

NSLog(@"%@ == %@ , nr.object =%d",[AnotherArray objectAtIndex:indexPath.row], [selektuara objectAtIndex:i], numObjects);

As numObjects is NSUInteger you should use %d to print its value.

Error in comparing:

if ([[selected objectAtIndex:i] compare:[AnotherArray objectAtIndex:indexPath.row]] == NSOrderedSame)

You shouldn't use == for comparing objects in Objective-C

Upvotes: 2

Related Questions