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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:43:08+00:00 2026-06-10T09:43:08+00:00

In the following code, I don’t get any warning or compile error if I

  • 0

In the following code, I don’t get any warning or compile error if I return NSMutableArray instead of NSArray which is the method’s return type.

But is it good practice to convert return value to NSArray by using -copy method like the following code?
Or should I return NSMutableArray?

+(NSArray *)returnImutableArray {
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    [arr addObject:@"a"];
    [arr addObject:@"b"];

    //return arr; // no warning
    return [arr copy];
}
  • 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-10T09:43:10+00:00Added an answer on June 10, 2026 at 9:43 am

    I think this is essentially personal preference. You incur a (probably small) performance penalty by copying the array before returning it. The upside is that you’ll completely prevent mutation of the result further down the line. However, I don’t usually go to the trouble. Because the declared return type of the method is an immutable NSArray, the compiler will warn you if you try to call one of NSMutableArray’s mutation methods on it unless you go out of your way to prevent that (by casting to NSMutableArray).

    So, in short, it’s personal preference and I personally don’t generally bother with the immutable copy.

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

Sidebar

Related Questions

I tried the following code. Although I don't get any errors, it did not
The following code produces the result I don't understand this. Any ideas why? If
I don't understand why the following code generates a warning. interface Generic<T> { }
The following code fails to compile (using VS2010) and I don't see why. The
Hi following Code gives a Syntax Error.I don't know how to fix the Problem.
I don't understand how the following code compiles/doesn't compile: struct Temp { int i;
The following code runs successfully with no errors, but I still don't get the
The following code don't work- TextView myLocation = new TextView(this); myLocation.setText(Sodala, Jaipur, Rajasthan, IN);
I came across following code and don't know what does having from twice mean
The following code crashes and burns and I don't understand why: DateTime dt =

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.