Header Ads

data storage NSDictionary and NSMutableDictionary - Objective C

NSDictionary  & NSMutableDictionary

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

Important methods of NSArray are as follows:
  • alloc/initWithObjctsAndKeys: Initalized a newly allocated dictionary with entries constructed from the specific set of values and keys.
  • valueForKey: return the value associated with a given key.
  • count: return the number of entries in the dictionary..
 NSMutableDictionary is inherited from NSDictionary and hence all instance methods of NSDictionary is available in NSMutableDictionary.

Important methods of NSMutableDIctionary are as follows:
  • removeAllObjects : Empties thedictionary 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.
We have to remember that the above list is just the freuently used methods and we can jump into respective classes in our xCode to know more methods in these class. A simple example is shown below:

#import <Foundation/Foundation.h>
int main()
{
        NSDictionary *dictobj = [[NSdictionary alloc]initWithObjectsAndKeys : @"raj",@"key1", @"ram",@"key2",@"rahul",@"key3",nil];
       NSString *strobj = [[dictobj objectForKey: @"key1"];
       NSLog(@"The object for key, key1 in dictionary  is %@", strobj);
     
       NSMutableDictionary *mutdictobj = [[NSMutableDictionary alloc]init];
       [mutdictobj setValue : @"raj" forKey:@"key1"];

       strobj = [ mutdictobj objectForKey : @"key1"];
      NSLog(@"The object for key ,key1 in mutableDictioary is : %@", strobj);    
     return 0;
}

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

No comments:

Powered by Blogger.