Objective-C: How to create an NSDictionary Object in standard way and collection literal way?

Akarsh Seggemu
1 min readJul 24, 2022

NSDictionary is part of the collections in Objective-C. It allows retrieving an object by using a key from a Dictionary. NSDictionary can also be represented using collection literal in Objective-C.

Let’s look at how to create an NSDictionary in Objective-C,

  • The standard way to create an NSDictionary,
NSDictionary *standardWay = [[NSDictionary alloc] initWithObjectsAndKeys: @"value", @"key", nil];
  • The collection literal way to create an NSDictionary,
NSDictionary *collectionLiteralWay = @{@"key": @"value"};

Let’s look at how to access an object from NSDictionary in Objective-C,

  • The standard way to access an object in NSDictionary,
[standardWay objectForKey:@"key"];
// output is
// value
  • The collection literal way to access an object in NSDictionary,
collectionLiteralWay[@"key"];
// output is
// value

I hope my article helps you understand how to create an NSDictionary object in Objective-C.

If you like my articles, please follow me. You can also support me by buying me a coffee.

--

--

Akarsh Seggemu

IT Team Lead | MSc in Computer Science from Technische Universität Berlin | Writing articles to Empower Software Engineers and IT Leaders