我有以下代码
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个相同的值,为什么?
答案 0 :(得分:4)
嗯,我认为你的装置完全搞砸了。从指针NSLog可以看出,实际上每次都会添加相同的可变字符串。但是当你在集合中时,你也在改变它。这必然意味着字符串的哈希值会发生变化,使用isEqual:
的结果也会发生变化,据我所知,这意味着该集合可能无法再找到已存在的对象。
为什么你在这里使用可变字符串,特别是因为你已经创建了一个完全可接受的不可变字符串(每次迭代两次)?像这样更改你的代码:
NSCountedSet *set = [[NSCountedSet alloc] init];
NSString *lDesktopPath = @"/Users/new/Desktop/";
int i= 10;
while (i)
{
lDeskTopPath = [NSString stringWithFormat:@"%@%d",[lDesktopPath stringByDeletingLastPathComponent], i];
NSLog(@"%@", lDesktopPath);
NSLog(@"%p", lDesktopPath);
[set addObject:lDesktopPath];
NSLog(@"%@", set);
--i;
}
NSLog(@"%@", set);