我必须遗漏一些基本的东西 - 希望有人可以向我指出。
我有两个NSSets。集合A包含40个NSNumber值,集合B包含20个NSNumber值。集合A包含集合B的20个值以及集合未共享的20个其他值。
我想做的是:NSSet * newSet = Set A - Set B,其中新集合仅包含集合B中未包含的集合A中的20个值。虽然我使用'minusSet',但我的新set始终包含所有40个原始值。我已经记录了每组A和B的内容,以确保它们包含我上面描述的内容。
这是我的代码:
1)创建集合A(包含40个NSNumber)
NSMutableSet *fetchedThreadIds = [NSMutableSet setWithArray:[threadedJsonMessages valueForKey:@"tid"]];
2)创建集合B(包含20个也在集合A中的NSNumber)
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
)
减号调用前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
答案 0 :(得分:5)
我尝试了这个并且它有效,所以我猜测还有其他代码缺失或者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)
主要是为格式化而发布的答案。