我有以下代码
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);
输出:
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])
我期待最终集中有 10 个不同的值,但最终集只包含 3 个相同的值,为什么?