NSCountedSet has no objects

175 Views Asked by At

I am trying to count the number duplicate objects that I have in an array. When I pass the array to my NSCountedSet function, it has no objects. From the examples I have seen, the usually pass NSArray to NSCountedSet where as I am passing an NSMutableArray. But, I can find no documentation that says that is not allowed.

-(void)GetDays
{
       ...
       BOOL goodDuplicates = NO;
       NSMutableArray *mostDaysAttended = [[NSMutableArray alloc] init];
       mostDaysAttended = [self gatherDays];
       goodDuplicates  = [self checkDuplicatesforIndex:mostDaysAttended];
       ...

{


-(BOOL)checkDuplicatesforIndex: (NSMutableArray *)mostDaysAttended
{


    NSCountedSet *set   = [[NSCountedSet alloc] initWithArray:mostDaysAttended];

    for (id item in set)  //<== at this point "set" has zero objects
    ...
    return(...)
}
2

There are 2 best solutions below

1
On BEST ANSWER

This is the code after the "NSCountedSet" declaration and before the "for" loop.

  //INITIALIZE
  for( int g =0;g<=11;g++)
    indexCount[g]=0;

Once I removed this loop and placed it at the beginning of the function before all of the other declarations, the code began working as expected. I am not sure why this would have affected the number of objects in the set, but it did.

4
On

My guess is that mostDaysAttended is being deallocated, so when you call checkDuplicatesIndex, there's nothing in there. You could easily check this by logging mostDaysAttended before your NSCountedSet *set..... line. If that turns out to be the case, create a retained (or strong) property for mostDaysAttended.