gpt4 book ai didi

objective-c - NSCountedSet 如何工作?

转载 作者:行者123 更新时间:2023-12-03 17:31:57 26 4
gpt4 key购买 nike

我有以下代码

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

最佳答案

好吧,我认为你的设定完全搞砸了。从指针 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);

关于objective-c - NSCountedSet 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10757090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com