Darius Miliauskas Darius Miliauskas - 2 months ago 5
Objective-C Question

What is the most reliable and fastest type or data structure to store data using Objective C?

I am looking for the type or data structure to store a big number of same type primitives on my app (Mac OS X or iOS) using Objective C. As I understood NSNumber stores only one primitive (correct me if I am wrong). I have, let's say, thousands of integers or strings. Which would be the best solution to put, store and access them there? NSSet, NSArray, NSMutableArray, NSDictionary, NSMutableDictionary or something else? I know that they have different features, but I care basically only about the performance of basic operations (putting, storing, retrieving).

Answer

It only depends on how you want to ADD, STORE and REMOVE this data.

First Let us go through each type of Data Structure that is available to us in Objective-C:

Primitive Array

This is the most basic type of storage in Objective-C(or C) which is used to store primitives. Ex: int a[4] = {1, 2, 3, 4}; The limitation to this is

  • Can only store primitive types.
  • Array size cannot be changed once declared.
  • Can only be retrieved by it index.
  • Can store only single type of data, defined at the time of declaring the array.

NSArray

This is a container for storing objects. Any object which is of type NSObject (or inherits from NSObject) or is of type 'id' can be stored in NSArray.

  • Once initialized, it cannot be mutated i.e. array size cannot be changed nor the objects it contains can be modified. This is good in terms of security.
  • Objects can only be accessed by its index.

NSMutableArray

Same as NSArray, but

  • Can be mutated, i.e. the existing objects can be modified and also new objects can be added or deleted.

NSSet

Same as NSArray but

  • Stores only unique objects.
  • Objects cannot be accessed by its index. Objects can only be accessed by enumeration.

NSMutableSet

Same as NSSet, but

  • Can be mutated, i.e. objects can be added or removed at a later point of time.

NSOrderedSet

Same as NSArray, i.e. objects are stored and retrieved by an index, but

  • Stores only unique objects.

NSMutableOrderedSet

Same as NSMutableArray, but

  • Stores only unique objects.

NSDictionary

Can store any type of data.

  • Objects are stored and retrieved by a key.
  • Once initialized, cannot be mutated i.e. cannot add new key-values nor can update existing objects associated to a particular key.

NSMutableDictionary

Same as NSDictionary

  • Can be mutated, i.e. new objects can be added or removed and existing objects can be modified.

This was a short description about mostly used Data Structures in Objective-C. These are used based on the need of the program and how data is to be manipulated.

Therefore,

  • If you want to store thousands of numbers and strings and want access it by its index value then use NSMutableArray. If you are not going to add, remove or modify any objects in the future then use NSArray.
  • If you want to store data but do not want duplicates and want to access it by its index the use NSOrderedSet/NSMutableOrderedSet
  • If you want to store data but do not want duplicates and its order also doesn't matter then use NSSet/NSMutableSet.
  • If you want to access data by a particular key then use NSDictionary/NSMutableDictionary

Regarding Performance

  • Since NSSet doesn't contain any order, they are more performant than NSArray
  • Here is a very good and detailed article on performance characteristics for each Data Structure discussed above

    Class                      Time [ms]    1,000,000 elements
    

    Adding

    NSMutableOrderedSet         3190.52
    NSMutableDictionary         2522.47
    NSMutableSet                2511.96
    NSMutableArray              1423.26
    NSSet                          8.03
    

    Random Access

    NSMutableOrderedSet           10.74
    NSMutableDictionary            9.18
    NSMutableArray                 8.08
    NSMutableSet                   4.47
    NSSet                          3.56
    

To know more about Objective-C Data Types and Data Structure, read this

Comments