Friday, June 21, 2013

NSMutableSet minusSet example in Objective C (iOS).

NSMutableSet minusSet

Removes each object in another given set from the receiving set, if present.

- (void)minusSet:(NSSet *)otherSet

Parameters of [NSMutableSet minusSet]
The set of objects to remove from the receiving set.

NSMutableSet minusSet example.
If duplicate items are not significant in the arrays, you can use the minusSet: operation of NSMutableSet:

NSMutableArray *firstArray = [NSMutableArray arrayWithObjects:@"Bill", @"Ben", @"Chris", @"Melissa", nil];
NSMutableArray *secondArray = [NSMutableArray arrayWithObjects:@"Bill", @"Paul" nil];

NSSet *firstSet = [NSSet setWithArray:firstArray];
NSMutableSet *secondSet = [NSMutableSet setWithCapacity:[secondArray count]];
[secondSet addObjectsFromArray:secondArray];

NSSet *result = [secondSet minusSet:firstSet];

Example of [NSMutableSet minusSet].
NSMutableSet *countriesSet = [NSMutableSet setWithArray:countriesArray];
NSSet *unSet = [NSSet setWithArray:unCountriesArray];
[countriesSet minusSet:unSet];
// countriesSet now contains only those countries who are not part of unSet

NSMutableSet minusSet example.
The your code could be:

NSMutableSet *web = [[NSMutableSet alloc] initWithObjects:@"2",@"3",@"4",nil];
NSMutableSet *disk = [[NSMutableSet alloc] initWithObjects:@"1",@"2",@"3",nil];

NSMutableSet * remArray = [[NSMutableSet setWithSet:disk] minusSet:web];
NSMutableSet * addArray = [[NSMutableSet setWithSet:web] minusSet:disk];
You can also read the Collections Programming Topics guide. Before using an array you have to determine if you want an ordered collection or not.

End of NSMutableSet minusSet example article.