gpt4 book ai didi

objective-c - NSMutableSet 设置减号设置不起作用

转载 作者:行者123 更新时间:2023-12-04 22:00:30 24 4
gpt4 key购买 nike

我一定错过了一些基本的东西 - 希望有人能向我指出。

我有两个 NSSet。集合 A 包含 40 个 NSNumber 值,集合 B 包含 20 个 NSNumber 值。集合 A 包含集合 B 的所有 20 个值以及这些集合未共享的 20 个其他值。

我想要做的是: NSSet *newSet = Set A - Set B,其中新集仅包含集 A 中不包含在集 B 中的 20 个值。虽然我使用的是“minusSet”,但我的新集set 始终包含全部 40 个原始值。我已经记录了每组 A 和 B 的内容,以确保它们包含我上面描述的内容。

这是我的代码:

1)创建集合A(包含40个NSNumber)

NSMutableSet *fetchedThreadIds = [NSMutableSet setWithArray:[threadedJsonMessages valueForKey:@"tid"]];

2)创建集合B(包含20个NSNumber,它们也在集合A中)

NSSet *savedThreads = threadIdsForSavedThreads([fetchedThreadIds allObjects]);

3) 尝试从 A 组中减去 B 组:

[fetchedThreadIds minusSet:savedThreads];

4) 将所有剩余对象添加到新数组中:

threadIdsForMessageFetch = [fetchedThreadIds allObjects];

但是,threadIdsForMessageFetch 包含 40 个对象,而不是 20 个。当我打印每个集合时,我得到:

设置A

{(
2540000000136987400,
2540000000141777055,
2540000000144609986,
2540000000125243232,
2540000000144610000,
2320000003021796109,
2540000000144375484,
2540000000136185765,
2540000000135311778,
2540000000135311816,
2540000000124128112,
2540000000125012438,
2480000001401011569,
2540000000144610015,
2540000000135311744,
2540000000139755359,
2540000000135311848,
2540000000143846308,
2540000000145732667,
2540000000143985955,
2540000000136185740,
2540000000135756434,
2540000000134373148,
2540000000140816249,
2540000000130860748,
2540000000127140093,
2540000000143526120,
2540000000102176392,
2540000000144609962,
2540000000128826163,
2540000000136185694,
2540000000133208691,
2540000000144555891,
2540000000138091838,
2540000000130179358,
2540000000126098342,
2540000000145162237,
2540000000131688927,
2540000000136185715,
2540000000143526101
)}

B组

{(
2540000000136987400,
2540000000138091838,
2540000000143985955,
2540000000144555891,
2540000000140816249,
2540000000145732667,
2540000000144609986,
2540000000144610000,
2540000000144609962,
2540000000144610015,
2540000000141777055,
2540000000143526101,
2540000000143526120,
2480000001401011569,
2540000000136185694,
2540000000145162237,
2540000000143846308,
2320000003021796109,
2540000000144375484,
2540000000139755359
)}

新数组:

<__NSArrayI 0xc4b8e40>(
2540000000136987400,
2540000000141777055,
2540000000144609986,
2540000000125243232,
2540000000144610000,
2320000003021796109,
2540000000144375484,
2540000000136185765,
2540000000135311778,
2540000000135311816,
2540000000124128112,
2540000000125012438,
2480000001401011569,
2540000000144610015,
2540000000135311744,
2540000000139755359,
2540000000135311848,
2540000000143846308,
2540000000145732667,
2540000000143985955,
2540000000136185740,
2540000000135756434,
2540000000134373148,
2540000000140816249,
2540000000130860748,
2540000000127140093,
2540000000143526120,
2540000000102176392,
2540000000144609962,
2540000000128826163,
2540000000136185694,
2540000000133208691,
2540000000144555891,
2540000000138091838,
2540000000130179358,
2540000000126098342,
2540000000145162237,
2540000000131688927,
2540000000136185715,
2540000000143526101
)

调用 minus set 之前 A 和 B 的计数如下:

2014-05-14 22:37:00.900 App[35877:60b] fetchedThreadIdsCount = 40
2014-05-14 22:37:00.900 App[35877:60b] savedThreadsCount = 20

最佳答案

我尝试了这个并且它有效,所以我猜测缺少其他代码或者 NSNumbers 确实不一样,只是打印的表示是相同的。

NSMutableSet *fetchedThreadIds = [NSMutableSet setWithObjects:@1, @2, @3, @4, nil];
NSSet *savedThreads = [NSSet setWithObjects:@2, @3, nil];
NSLog(@"fetchedThreadIds: %@", fetchedThreadIds);
NSLog(@"savedThreads: %@", savedThreads);

[fetchedThreadIds minusSet:savedThreads];
NSLog(@"fetchedThreadIds: %@", fetchedThreadIds);
NSArray *threadIdsForMessageFetch = [fetchedThreadIds allObjects];
NSLog(@"threadIdsForMessageFetch: %@", threadIdsForMessageFetch);

NSLog 输出:

fetchedThreadIds: {(
3,
2,
1,
4 )}
savedThreads: {(
3,
2 )}
fetchedThreadIds: {(
1,
4 )}
threadIdsForMessageFetch: (
1,
4 )

作为主要用于格式化的答案发布。

关于objective-c - NSMutableSet 设置减号设置不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23668201/

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