Collections in Objective-C are things such as arrays and sets. They are used to contain objects (usually of the same nature) inside a container. There are many advantages to using collections.
Collections in Objective C: Arrays
Above all, the primary advantage of an array is that it has an index that orders objects. As a result, an array index begins at 0 and counts upwards. To use NSArray, we need to both allocate and initialize it. Here is an example of NSArray:
// NSArray Sample NSArray* myArr; myArr = [[NSArray alloc] initWithObjects: @"Monday", @"Tuesday", @"Wednesday", nil];
We can use count to get the number of elements in an array. We can access the different elements of an array using either objectAtIndex or using a subscript ( [] ).
However, NSArrays are immutable so you can not add new objects to them. For adding or removing objects into an array, we use mutable arrays.
Collections in Objective C: Sets
On the other hand, we have sets. Sets are very similar to an array except that they don’t have an order. As a result the objects in a set can not be duplicates and must unique. Here is an example:
// NSSet Sample NSSet* mySet = [[NSSet alloc] initWithObjects:@"one", @"two", nil];
Because of the nature of the sets, to find objects in a set we need to iterate through them. You could also use the C arrays. These are arrays made of any kind of object such as int such as this:
// C Style Arrays int intArray[6] = {1, 2, 3, 4, 5, 6};
Objects in an array do NOT have to be of the same type.
Collections in Objective C: Dictionaries
Dictionaries are a type of collection in Objective-C that have a key-value pairing between them. As a result each value is paired to a certain key and can be searched and found using that key. You can use the NSDictionary to initialize a dictionary such as:
// NSDictionary Sample NSDictionary *myDict; myDict = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];
As a result, you would most likely be using a NSMutableDictionary such as:
// NSMutableDictionary Sample NSMutableDictionary *myMutDict = [NSMutableDictionary new]; [myMutDict setObject:@"Amir" forKey:@"Name"];
In Addition, to access a value for a certain key, you would be doing:
// Accessing Dictionary Objects [myMutDict objectForKey:@"Name"];
Similarly, you could also initialize a dictionary with multiple keys and values at once, subsequently you would write code like the following:
// Initializing a Dictionary NSDictionary *multipleDict; multipleDict = [NSDictionary dictionaryWithObjectsAndKeys: @"Value 1", @"Key1", @"Value 2", @"Key2", nil];
Finally, it’s worth noticing that the keys are arbitrary values and you can use whatever you like. For instance, can get all the keys:
// All Dictionary Keys [myMutDict allKeys];
Or in other words, we could get all the values:
// All Dictionary Values [myMutDict allvalues];
Dictionaries can be initialized in many different ways.
Summary
In conclusion, we user arrays, dictionaries and sets as collections in objective-C. You can nest arrays and dictionaries within one another.
Interested in learning more in-depth concepts?
If you are interested in learning more about mobile app development, I invite you to join my complete iPhone or Android course(s):