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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:40:53+00:00 2026-05-23T04:40:53+00:00

I hardly ever see the second one used and I wonder why? Neither would

  • 0

I hardly ever see the second one used and I wonder why?

  • Neither would it break support for situations where an NSArray is expected (as it’s a subclass).
  • Nor would it break encapsulation by revealing mutable internals.

Under the precondition that it’s never a mutable ivar that’s returned, (which should be common sense anyway)
I can right now only think of advantages of using the second.

  • It actually is mutable. And muting is safe here, so why prevent it?
  • No need to call [[[foo fooBar] mutableCopy] autorelease], which needlessly allocates additional memory and needlessly wastes time.

Here are the method variations:

- (NSArray *)fooBar {
    NSMutableArray *fooArray = [NSMutableArray array];
    //populate fooArray
    return fooArray;
}

- (NSMutableArray *)fooBar {
    NSMutableArray *fooArray = [NSMutableArray array];
    //populate fooArray
    return fooArray;
}

I’m asking as my project has a bunch of methods with the same pattern.
And in most of the times the returned array will be modified afterwards (merged, edited, etc).
So I think it should be totally fine to return NSMutableArrays, yet nobody seems to be doing it.

NSMutableArray, NSMutableSet, NSMutableDictionary… it’s basically the same deal.

  • 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-23T04:40:53+00:00Added an answer on May 23, 2026 at 4:40 am

    I suppose the first variation was preferred because polymorphism was preferred.

    In either case, both methods return an instance of NSMutableArray, the only difference being that the first one hides that fact from the caller. In other words, the first variation is not safer than the second. It’s essentially using polymorphism to tell the caller that any type of NSArray might be returned. If you need that kind of flexibility in your code, it definitely has it’s advantages. (e.g., if one day, for whatever reason, you need to return a custom NSArray subclass, your code won’t break at that level).

    However, you seem to prefer communicating intent to the caller – i.e. that you actually return mutable arrays – which is also OK. To make everyone happy (if there is such thing anyways…), I suggest renaming the 2nd method to:

    - (NSMutableArray *)mutableFooBar {
        NSMutableArray *fooArray = [NSMutableArray array];
        //populate fooArray
        return fooArray;
    }
    

    As a side note, I think that the following is a slightly more efficient way to convert an existing immutable array into a mutable one:

    NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:fooArray];
    

    (correct me if I’m wrong on that assumption).

    I hope this answers your question…

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

Sidebar

Related Questions

I have a lightly used dictionary which is hardly ever going to be read
I hardly see any pointer on the following problem related to Hibernate. This pertains
Coming from an svn background: I hardly ever branched, due to the (lack of)
I hardly even know how to ask this one, but here goes. I have
The title is hardly understandable, but I'm not sure how to summarize that another
There is so much written about unit testing but I have hardly found any
Before I start, I should let you know that I'm hardly experienced in PHP.
I am new on iphone development and I have problem with CoreData. I hardly
I know, relational database is a database where fields in one table are linking
I need to define a one-to-one relationship, and can't seem to find the proper

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.