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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:26:40+00:00 2026-05-18T00:26:40+00:00

Would someone be so kind as to confirm that I am understanding this correctly.

  • 0

Would someone be so kind as to confirm that I am understanding this correctly. My initial response to this was that the mutableArray when assigned to an immutableArray would become an immutableArray, however this is not the case.

Is this because the initial array is allocated as a mutableArray and the immutableArray is just assigned to point to the same object. The compiler gives a warning, but the the code executes just fine at runtime.

NSMutableArray *mArray = [NSMutableArray arrayWithObjects:@"Teddy", @"Dog", nil];
NSArray *iArray = mArray;
[iArray addObject:@"Snoss"]; // Normally this would crash NSArray

much appreciated

gary.

  • 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-18T00:26:41+00:00Added an answer on May 18, 2026 at 12:26 am

    Don’t confuse the physical object that you just created, with how you are effectivley casting it in your code.

    In this case, you did physically create a NSMutableArray.

    Then you effectivley cast it to an NSArray – which is totally valid – for example, there are many cases where a function might want an NSArray, and you can pass it an NSArray, or anything derived from it (like an NSMutableArray).

    The problem is that you get a compiler warning because you are trying to call addObject on an object which the compiler thinks is just an NSArray because that’s what it’s physical type is.

    This will actually work, because it actually is an NSMutableArray, and in runtime would respond to that selector.

    It’s bad coding practice to do it this way, because NSArray doesn’t actualy respond to the addObject selector. For example, I could create a function, like:

    -(void) addIt:(NSArray)myThing {
      [myThing addObject:@"New String"];
    }
    

    The compiler would give you a warning saying the “NSArray” doesn’t respond to the “addObject” selector. If you cast an NSMutableArray, and passed it to this function, it myThing, it would actually work.

    It’s bad practice though, because if you actually passed an NSArray it would crash.

    Summary: Don’t confuse what the object really is vs. what you are making the compiler interpret it as.

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

Sidebar

Related Questions

I joined this site today hoping someone would be kind enough to explain to
Having trouble finding an explanation in the docs, would someone be kind enough to
would someone please write this code: this.Loaded += (s, e) => this.loaded = true;
Would someone be so kind as to explain what is happening with the statement
I have one location match that correctly works: <LocationMatch ^/user/storage/subaccounts(/[a-zA-Z0-9]+)?/?$> this correctly forwards for
Would someone be able to break down the Ruby specifics of what each of
Would someone be able to help here? I would like to fill a div
Would someone kindly explain why the following bit of code works, I've tested it
would someone be able to help me with a button to clear /delete the
How would someone go about adding check boxes to only the children of a

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.