0

我有以下代码

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 个相同的值,为什么?

4

1 回答 1

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);  
于 2012-05-25T15:41:30.187 回答