Question

I must be missing something fundamental - hopefully someone can point it out to me.

I have two NSSets. Set A contains 40 NSNumber values and set B contains 20 NSNumber values. Set A contains all of set B's 20 values plus 20 other values not shared by the sets.

What I'd like is to do: NSSet *newSet = Set A - Set B, where the new set only contains the 20 values from set A not contained in set B. Although I am using 'minusSet', my new set always contains all 40 of the original values. I have logged the contents of each set A and B to make sure that they contain the contents that I described above.

Here is my code:

1) Create set A (contains 40 NSNumbers)

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

2) Create set B (contains 20 NSNumbers which are also in set A)

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

3) Attempt to subtract set B from set A:

[fetchedThreadIds minusSet:savedThreads];

4) Add all remaining objects to new array:

threadIdsForMessageFetch = [fetchedThreadIds allObjects];

However, threadIdsForMessageFetch contains 40 objects rather than 20. When I print each set I get:

Set 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
)}

Set B

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

New Array:

<__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
)

The counts of A and B before the minus set call are as follows:

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

Solution

I tried this and it worked so I'm guessing there is other code that is missing or the NSNumbers really aren't the same, just the printed representation is the same.

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 output:

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

Posted as an answer mainly for formatting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top