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

  • Home
  • SEARCH
  • 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 7690365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:22:44+00:00 2026-05-31T20:22:44+00:00

NSArray *array = [dictionary objectForKey:@field]; and NSArray *array = [[NSArray alloc] initWithArray:[dictionary objectForKey:@field]]; I

  • 0
NSArray *array = [dictionary objectForKey:@"field"];

and

NSArray *array = [[NSArray alloc] initWithArray:[dictionary objectForKey:@"field"]];

I see both kind of approaches very frequently in objective C code.
When tried to understand, I found both of them used in similar situation too, which makes contradiction. I am not clear on when I should use 1st approach and when 2nd one?
Any idea?

Detailed explanation and useful references are moms welcome.

  • 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-05-31T20:22:45+00:00Added an answer on May 31, 2026 at 8:22 pm

    First off, those two examples are doing slightly different things. One is retrieving something from an existing dictionary and one is creating a new array by retrieving something from an existing dictionary (the value of that key is an array).

    But, if you’re asking the difference between getting objects by alloc vs. convenience methods. ([NSString alloc] init vs [NSString stringWith …), by convention, you own anything that you call alloc, new copy or mutableCopy on. Anything that you call that is not those, is autoreleased.

    See the memory guide here. Specifically, look at the rules.

    Getting an autoreleased object means it will go away at some point in the near future. If you don’t need to hold onto outside the scope of that function, then you can call autorelease on it or use one of the convenience methods that’s not alloc, etc…

    For example:

    // my object doesn't need that formatted string - create the autoreleased version of it.
    - (NSString) description {
        return [NSString stringWithFormat:@"%@ : %d", _title, _id]; 
    }
    
    // my object stuffed it away in an iVar - I need the retained version of it.  release in dealloc
    - (void) prepare {
        _myVal = [[NSString alloc] initWithFormat:"string I need for %d", _id];
    }
    

    In the first example, I created a convenience methods for others to call, my class doesn’t need that object beyond the scope of that method so I create the autoreleased version of it and return it. If the caller needs it beyond the scope of his calling method, he can retain it. If not he can use it and let it go away. Very little code.

    In the second example, I’m formatting a string and assigning it to an iVar variable that I need to hold onto for the lifetime of my class so I call alloc which will retain it. I own it and releasing it eventually. Now, I could have used the first version here and just called retain on it as well.

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

Sidebar

Related Questions

What is the advantage of doing this: NSArray *array = [[NSArray alloc] initWithObjects:@Year, @Capital,
My code: NSString *jsonString; jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; // Create a dictionary
When I was using an NSArray, it was easy: NSArray *array = ... lastIndex
I have an NSArray of Foos in an Objective-C program. I would like to
How to convert NSArray into NSString in objective-c?
I am relatively new to Objective C and need some array help. I have
I'm new to Objective-C and I'm trying to create a simple dictionary style app
When using Clang static analyzer to analyze my Objective-C code for iOS, I get
I'm having trouble in writing mutable dictionary to a file. Here's the code that
I have the following code that decodes a JSON string into an array of

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.