0

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(...)
}
Kevin McFadden
  • 337
  • 1
  • 5
  • 23

2 Answers2

0

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.

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • I logged the count of mostDaysAttended and of set just prior to the "for" loop and the both contained the same value of "7". Hoever, when debugging the code using breakpoints and hovering over the "set" value in the "for" loop, it displays '0x0 0 objects'. COuld there be an issue in my "for" condition. THis is really baffling. – Kevin McFadden Sep 15 '12 at 00:56
  • So, you're saying that if you log set.count right before the for loop you get 7, and if you log it just after the for (id item in set) line you get 0? – rdelmar Sep 15 '12 at 01:11
  • Yes. I aded nome more logs. Right before my "for" loop, I had a piece of code that form 0 to 11 that initialized a small array with zeros. When I commented out this loop, the set retained it's value. Weird. – Kevin McFadden Sep 15 '12 at 15:40
  • So, is the problem solved? If not, you should post the actual code that you're using now, including the logs and this code piece you mention above. – rdelmar Sep 15 '12 at 15:59
0

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.

Kevin McFadden
  • 337
  • 1
  • 5
  • 23
  • Where and how did you initialize indexCount, because I dropped this code into my test project, and it made no difference -- the set still showed the correct number of items. Did you try moving it back to the "problem" position to see if the problem came back? – rdelmar Sep 16 '12 at 22:48