Quantcast
Channel: User Burhanuddin Sunelwala - Stack Overflow
Viewing all articles
Browse latest Browse all 36

Answer by Burhanuddin Sunelwala for What is the most reliable and fastest type or data structure to store data using Objective C?

$
0
0

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 its 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.52NSMutableDictionary         2522.47NSMutableSet                2511.96NSMutableArray              1423.26NSSet                          8.03

    Random Access

    NSMutableOrderedSet           10.74NSMutableDictionary            9.18NSMutableArray                 8.08NSMutableSet                   4.47NSSet                          3.56

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


Viewing all articles
Browse latest Browse all 36

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>