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:
Important methods of NSMutableDIctionary are as follows:
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.
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..
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.
#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;
}
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: