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 8668261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:10:10+00:00 2026-06-12T18:10:10+00:00

To return a NSArray or NSDictionary, I have seen most people use the below

  • 0

To return a NSArray or NSDictionary, I have seen most people use the below implementation and this is also what some books suggest. (iOS Development A Practical Approach – )

OPTION 1

  -(NSArray*)listOfStudents{

    NSMutableArray *temp = [[NSMUtableArray alloc] init];

    //Add elements to the array
    //
    //
    //
    NSArray *students =  [NSArray arrayWithArray:temp];

    return students;
    }



 -(void)viewWillAppear{

    self.studentsList = [self listOfStudents];
    }

But can this same be done by the below way also?

OPTION 2

 -(NSArray*)newListOfStudents{

    NSMutableArray *temp = [[NSMUtableArray alloc] init];
    NSArray *students = [[NSArray alloc]initWithArray:temp];
    [temp release];
    //Add elements to the array
    //
    //
    //


    return students;
    }


-(void)viewWillAppear{



  NSArray *array = [self newListOfStudents];
    self.studentsList = array;
    [array release];

    }

Assume these methods are called in the main thread itself.

Interms of memory usage , I think that the second option is good, because it does not create autoreleased objects, because they are released only at when the autorelease pool is drained.

I assume that the main autorelease pool is drained only when the app quits. So if the method in OPTION 1 is used many times ,(since they are getting called in ViewWillAppear) I think that many lists will be in autorelease pool being released only when the app quits.

So is the OPTION 2 approach the better approach?

UPDATE:

I have updated the viewWillAppear implementation for better clarity.

  • 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-12T18:10:12+00:00Added an answer on June 12, 2026 at 6:10 pm

    I think in the second example you meant to call

    self.studentsList = [self newListOfStudents];
    

    In case that studentsList is a retained property, this would leak now.

    Also, that temp array in both examples is just useless overhead. In the second example it’s plain nonsense.

    The cleanest solution is

    -(NSArray *)listOfStudents {
       NSMutableArray *list = [NSMutableArray array];
       // Add things to array
       return list;
    }
    

    Two more advices:

    1) you might run the static analyzer over your code, which will point to memory issues.
    2) if you feel more confident with memory management, switch over to ARC.

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

Sidebar

Related Questions

i have function with return type as (NSArray/NSData/NSString/NSDictionary and so on). i can return
I have numerous classes that use the various NSDictionary/NSArray collection classes as ivars but
I have tried this but it crashes: - (NSDictionary *)contacts { NSMutableArray *result =
I want to write some values to an external file and use NSDictionary for
I have a NSArray of NSDictionary which is like the following. ({ a =
I have an if statement that's condition is passed to this implementation file via
I am trying to use the Category described in this article: http://iphonedevelopment.blogspot.com/2008/10/shuffling-arrays.html I have
I have a JSON return that I'm trying to save into a NSDictionary, However,
If I have this method: + (NSDictionary *)dictionaryFromQueryString:(NSString *)queryString { NSMutableDictionary *this = [[NSMutableDictionary
I have the following // this code is inside cellForRowAtIndexPath for a TableViewController id

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.