Objective-C: How to create a mutable dictionary?

  • A key is always unique, and it will not have any duplicates. As, it is used as an identifier to retrieve a value.
  • A value need not be unique, and it can be a duplicate. But its associated key is always unique.
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
// Empty dictionary stored in dict
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSLog(@"value of dict is %@",dict);

return 0;
}
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
@"value", @"key", nil];
NSLog(@"value of dict is %@",dict);
[dict setObject: @"value two" forKey: @"key-two"];
NSLog(@"value of dict is %@",dict);
return 0;
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akarsh SEGGEMU

Akarsh SEGGEMU

24 Followers

Software Engineer (Android, iOS, Unity) | M.Sc. Computer Science — Technische Universitaet Berlin | Mobile Advertising, Attribution, Monetisation, UA & Analytic