Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8618633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:09:17+00:00 2026-06-12T06:09:17+00:00

I am reading through the iOS Developer Guide to get familiarized with the Objective-C

  • 0

I am reading through the iOS Developer Guide to get familiarized with the Objective-C language and currently I am having a little confusion on the topic of Container Literals and Subscript Notation as it pertains to creating objects like NSDictionary.

I understand that there are several ways to create NSDictionary objects including Key-Value encoding (dictionaryWithObjects:forKeys: and dictionaryWithObjectsAndKeys:, or their corresponding initializers). Source Link.

From my understanding there are two main ways to do this and then there is another way which is by using container literals, demonstrated here:

NSDictionary *myDictionary = @{
   @"name" : NSUserName(),
   @"date" : [NSDate date],
   @"processInfo" : [NSProcessInfo processInfo]
};

Which is the best way to use? Is there any benefit in using the Container Literal technique over the previous two or is it just a convenience thing for programmers?

I am under the impression that it is also just another easier way to code things like arrays. Is this true or is there something that I’m missing here? Are these techniques just a matter of personal preference?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:09:18+00:00Added an answer on June 12, 2026 at 6:09 am

    I disagree with the other answers posted thus far: almost all the time, it’s better to use the new container literal syntax than to use constructors. They help with code correctness, and there’s not really that much to worry about for compatibility.

    Code Correctness

    Container literals are indeed syntactic sugar, but specifically they map to the “safe” constructor methods +[NSArray arrayWithObjects:count:] and +NSDictionary dictionaryWithObjects:forKeys:count:. Constructing an array or dictionary using one of these methods directly isn’t all that convenient, so many programmers find it simpler to use arrayWithObjects: and dictionaryWithObjectsAndKeys:. However, the latter methods have a nasty pitfall: since the argument list must be terminated with nil, you can find yourself with unexpected array/dictionary contents if you pass nil where you intend to pass an object.

    For example, say you’re setting up a dictionary mapping the properties of one of your model objects (maybe you’re going to send it as JSON?):

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
        person.name, @"name", person.title, @"title", person.address, @"address", 
        nil];
    

    If this code runs into a Person for whom no title has been set, the resulting dictionary will be missing the @"address"key and its value. You could spend hours tracking down why some fraction of the people in your database are missing addresses (and even see the code above and tear your hair out wondering why it’s not working when c’mon, I’m setting it right there!). Many of us have.

    By contrast, if you use the literal form like this:

    NSDictionary *dictionary = @{
        @"name": person.name, @"title": person.title, @"address": person.address };
    

    It will be expanded to something like this:

    id objects[] = { person.name, person.title, person.address };
    id keys[] = { @"name", @"title", @"address" };
    NSUInteger count = sizeof(objects) / sizeof(keys);
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
                                                           forKeys:keys
                                                             count:count];                          
    

    And if person.name or person.title returns nil, this method will throw an exception instead of silently creating data you don’t want. (Either way you’ll have to decide how you want your code to handle nil titles, but this way you’ll catch the problem sooner.) And sure, you could write this “safer” form yourself instead of using the equivalent syntactic sugar, but are you sure you won’t just fall back on the habit of writing dictionaryWithObjectsAndKeys: because it’s shorter?

    Compatibility

    The code generated by container literals (and number literals and boxed expressions, for that matter) uses no new API, so you can compile it with Xcode 4.4 or newer (or Clang 3.1 or newer directly) and deploy to any version of Foundation. You do need to consider compatibility if your source code will also be used with older compilers or GNUStep, however. (Though it sounds like GNUStep is good with Clang now, too.)

    And it’s not part of the question, but since it’s on a related subject: the same is “sort of” true for the new object subscripting syntax. That does use new methods only defined on Mac OS X 10.6 and iOS 6.0… but those methods are provided by libarclite. (You know, the library that gets linked in when you try to deploy ARC code back to iOS 4.3 or Mac OS X 10.6 — it’s not just for ARC anymore!) So all you need to do is declare them in a header, link ARCLite if you’re not already, and you’re good to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm reading through the Big Nerd Ranch iOS guide and I have a question
I'm currently reading through this jquery masking plugin to try and understand how it
I'm a newbie and am learning the iOS-dev through reading the official tutorial Your
When reading through this Async Sockets example , I find this code: // Get
I'm reading through the Big Nerd Ranch book on iOS programming and I had
Thanks for reading. I went through Facebook iOS SDK Feed Dialog issue after authentication
Reading through this excellent article about safe construction techniques by Brain Goetz, I got
Reading through some of the questions here, the general concensus seems to be that
Reading through the SendAsync , BeginAsync method illustrations of Socket s, I realized that
Reading through documentation, I found following: 1.9.1 1.8.4 1.8.2 A version of 1.8.2 select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.