Objective-C_Data Storage

  • NSArray
  • NSDictionary
  • NSSet

provide storage for Objective-C objects of any class.

NSArray & NSMutableArray

  • NSArray is used to hold an immutable array of objects
  • NSMutableArray is used to hold an mutable array of objects.

Note

  • Mutablility helps to change the array in runtime a preallocated array
  • NSMutableArray is inherited from NSArray and hence all instance methods of NSArray is available in NSMutableArray

Important methods of NSArray are as follows

  • alloc/initWithObjects: Used to initialize an array with objects.
  • objectAtIndex: Returns the object at specific index.
  • count: Returns the number of objects

Important methods of NSMutableArray are as follows:

  • removeAllObjects: Empties the array.
  • addObject: Inserts a given object at the end of the array.
  • removeObjectAtIndex: This is used to remove objectAt a specific index
  • exchangeObjectAtIndex:withObjectAtIndex: Exchanges the objects in the array at given indices.
  • replaceObjectAtIndex:withObject: Replaces the object at index with an Object.

Simple Example

In the above program, we have seen a simple differentiation between NSMutableArray and NSArray where we can insert a string after allocation in mutable array.

NSDictionary & NSMutableDictionary

NSDictionary is used to hold an immutable dictionary of objects and the NSMutableDictionary is used to hold an mutable dictionary of objects.

Important methods of NSDictionary are as follows

  • alloc/initWithObjectsAndKeys: Initializes a newly allocated dictionary with entries constructed from the specified set of values and keys.
  • valueForKey: Returns the value associated with a given key.
  • count: Returns the number of entries in the dictionary.

NSMutableDictionary is inherited from NSDictionary and hence all instance methods of NSDictionary are available in NSMutableDictionary

Important methods of NSMutableDictionary are as follows:

  • removeAllObjects: Empties the dictionary of its entries.
  • removeObjectForKey: Removes a given key and its associated value from the dictionary.
  • setValue:forKey: Adds a given key-value pair to the dictionary.

Simple Example

NSSet & NSMutableSet

NSSet is used to hold an immutable set of distinct objects and the NSMutableDictionary is used to hold an mutable set of distinct objects.

Important methods of NSSet are as follows:

  • alloc/initWithObjects: Initializes a newly allocated set with members taken from the specified list of objects.
  • allObjects – Returns an array containing the set’s members or an empty array if the set has no members.
  • count: Returns the number of members in the set.

NSMutableSet is inherited from NSSet and hence all instance methods of NSSet are available in NSMutableSet.

Important methods of NSMutableSet are as follows:

  • removeAllObjects: Empties the set of all of its members.
  • addObject: Adds a given object to the set, if it is not already a member.
  • removeObject: Removes a given object from the set.

Simple Example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: