I have following code
NSCountedSet *set = [[NSCountedSet alloc] init];
NSMutableString *lDesktopPath = [[NSMutableString alloc] initWithString:@"/Users/new/Desktop/"];
int i= 10;
while (i)
{
NSLog(@"%@", [NSString stringWithFormat:@"%@%d",[lDesktopPath stringByDeletingLastPathComponent],i]);
[lDesktopPath setString:[NSString stringWithFormat:@"%@%d",[lDesktopPath stringByDeletingLastPathComponent],i ]];
NSLog(@"%p", lDesktopPath);
[set addObject:lDesktopPath];
NSLog(@"%@", set);
--i;
}
NSLog(@"%@", set);
Output:
2012-05-25 20:41:37.619 NSCountableset[21955:a0f] /Users/new10
2012-05-25 20:41:37.621 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.623 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/Users/new10 [1])
2012-05-25 20:41:37.623 NSCountableset[21955:a0f] /Users9
2012-05-25 20:41:37.624 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.624 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/Users9 [1], /Users9 [1])
2012-05-25 20:41:37.625 NSCountableset[21955:a0f] /8
2012-05-25 20:41:37.625 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.625 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/8 [1], /8 [1], /8 [1])
2012-05-25 20:41:37.626 NSCountableset[21955:a0f] /7
2012-05-25 20:41:37.626 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.627 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/7 [1], /7 [1], /7 [2])
2012-05-25 20:41:37.627 NSCountableset[21955:a0f] /6
2012-05-25 20:41:37.628 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.628 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/6 [2], /6 [1], /6 [2])
2012-05-25 20:41:37.635 NSCountableset[21955:a0f] /5
2012-05-25 20:41:37.635 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.636 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/5 [2], /5 [2], /5 [2])
2012-05-25 20:41:37.636 NSCountableset[21955:a0f] /4
2012-05-25 20:41:37.640 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.640 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/4 [2], /4 [2], /4 [3])
2012-05-25 20:41:37.641 NSCountableset[21955:a0f] /3
2012-05-25 20:41:37.641 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.642 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/3 [3], /3 [2], /3 [3])
2012-05-25 20:41:37.642 NSCountableset[21955:a0f] /2
2012-05-25 20:41:37.643 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.643 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/2 [3], /2 [3], /2 [3])
2012-05-25 20:41:37.643 NSCountableset[21955:a0f] /1
2012-05-25 20:41:37.644 NSCountableset[21955:a0f] 0x100111ac0
2012-05-25 20:41:37.644 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/1 [3], /1 [3], /1 [4])
2012-05-25 20:41:37.644 NSCountableset[21955:a0f] <NSCountedSet: 0x1001114b0> (/1 [3], /1 [3], /1 [4])
I was expecting 10 distinct values in final set but final set contain only 3 same values, why ?
Well I think your set is totally screwed. As you can see from the pointer NSLog, you are actually adding the same mutable string every time. But you are also changing it while it is in the set. This necessarily means that the string's hash value changes and so will the result of using
isEqual:
which, as far as I know, means that the set may no longer be able to find objects that are already in it.Why are you using a mutable string here at all, especially since you already create a perfectly acceptable immutable string (twice per iteration)? Change your code like this: