Tuesday 22 July 2014

Collections of Objects: NSSet v/s NSArray v/s NSDictionary

Collection is a Foundation framework class used for storing and managing groups of objects.

Art/collections_intro_2x.png

NSSet v/s NSArray v/s NSDictionary

Arrays are ordered collections of any sort of object.

Dictionaries manage pairs of keys and values. A key-value pair within a dictionary is called an entry, the keys are unique.

Sets (such as NSSet , NSMutableSet, and NSCountedSet) are unordered collections of objects. 
Sets allow for fast insertion and deletion operations. 
They also allow you to quickly see whether an object is in a collection. 



NSSet and NSMutableSet store collections of distinct objects, 
while NSCountedSet stores a collection of non-distinct objects.
For example, suppose you have a number of city objects and you want to visit each one only once.
If you store each city that you visit in a set, you can quickly and easily see whether you have visited it.


An NSSet object manages an immutable set of distinct objects—that is, after you create the set, you cannot add, remove, or replace objects, but can modify individual objects themselves (if they support modification). The mutability of the collection does not affect the mutability of the objects inside the collection.
NSMutableSet, a subclass of NSSet, is a mutable set of distinct objects, which allows the addition and deletion of entries at any time, automatically allocating memory as needed.
NSCountedSet, a subclass of NSMutableSet, is a mutable set to which you can add a particular object more than once; in other words, the elements of the set aren’t necessarily distinct. A counted set(a bag)  keeps a counter associated with each distinct object inserted.
There is only one instance of an object in a counted set, even if the object has been added multiple times. 


The NSHashTable class is configured by default to hold objects much like NSMutableSet does. It also allows additional storage options that you can tailor for specific cases.
For example, the map table in Figure below is configured to hold weak references to its elements. You can also specify whether you want to copy objects entered into the set.
Figure   Hash table object ownership
You can use an NSHashTable object when you want an unordered collection of elements that uses weak references. For example, suppose you have a global hash table that contains some objects. Because global objects are never collected, none of its contents can be deallocated unless they are held weakly. Hash tables configured to hold objects weakly do not own their contents. If there are no strong references to objects within such a hash table, those objects are deallocated. For example, the hash table in Figure above holds weak references to its contents. Objects A, C, and Z will be deallocated, but the rest of the objects remain.
Usage: 
- Use an immutable set if the set rarely changes, or changes wholesale.
- Use a mutable set if the set changes incrementally, or is very large—as large collections take more time to initialize.
- Use counted set if one instance of an object in a set, has to added multiple times. 




@interface NSMutableSet (NSExtendedMutableSet)
- (void)addObjectsFromArray:(NSArray *)array;
- (void)intersectSet:(NSSet *)otherSet;
- (void)minusSet:(NSSet *)otherSet;
- (void)removeAllObjects;
- (void)unionSet:(NSSet *)otherSet;
- (void)setSet:(NSSet *)otherSet;


Eg:
    NSMutableSet *set1 = [[NSMutableSet alloc]initWithObjects:@"1",@"3",@"2", nil];
    NSLog(@"set1 before : %@",set1);

    NSMutableSet *set2 = [[NSMutableSet alloc]initWithObjects:@"1", nil];
    [set1 minusSet:set2];
    
    NSLog(@"set1 after deletion : %@",set1);

Output:-------
 set1 before : {(
    1,
    3,
    2
)}
set1 after deletion : {(
    3,
    2

)}





NSOrderedSet is available in iOS 5+ so with that the main difference becomes whether you want duplicate objects in the data structure.
Read More of NSOrderedSet