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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:46:16+00:00 2026-05-25T01:46:16+00:00

I read in Cocoa and Objective C: Up and Running that -copy will always

  • 0

I read in Cocoa and Objective C: Up and Running that -copy will always return an immutable object and -mutableCopy will always return a mutable object:

It’s important to know that calling -copy on a mutable object returns an immutable
version. If you want to copy a mutable object and maintain mutability in the new version,
you must call -mutableCopy on the original. This is useful, though, because if you want
to “freeze” a mutable object, you can just call -copy on it.

So I have something like this:

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] init];
NSLog( @"%@", [req className] );               // NSMutableURLRequest
NSLog( @"%@", [[req copy] className] );        // NSMutableURLRequest
NSLog( @"%@", [[req mutableCopy] className] ); // NSMutableURLRequest

According to this previous answer:

You cannot depend on the result of copy to be mutable! Copying an NSMutableArray may
return an NSMutableArray, since that’s the original class, but copying any arbitrary
NSArray instance would not.

This seems to be somewhat isolated to NSURLRequest, since NSArray acts as intended:

NSArray *arr = [[NSMutableArray alloc] init];
NSLog( @"%@", [arr className] );                 // __NSArrayM
NSLog( @"%@", [[arr copy] className] );          // __NSAraryI
NSLog( @"%@", [[array mutableCopy] className] ); // __NSArrayM

So…

  1. When does -copy return an immutable object (as expected) and when does it return a mutable object?
  2. How do I achieve the intended effect of getting a “frozen” copy of a mutable object that refuses to be “frozen”?
  • 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-25T01:46:17+00:00Added an answer on May 25, 2026 at 1:46 am

    I think you’ve uncovered a great rift between documentation and reality.

    The NSCopying protocol documentation claims:

    The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object; otherwise the exact nature of the copy is determined by the class.

    But this is clearly wrong in some cases, as you’ve shown in your examples (and I’ve sent feedback to them about this via that documentation page).

    But(#2) in my opinion, it doesn’t actually matter and you shouldn’t care.

    The point of -copy is that it will return an object you can use with the guarantee that it will behave independently of the original. This means if you have a mutable object, -copy it, and change the original object, the copy will not see the effect. (In some cases, I think this means that -copy can be optimized to do nothing, because if the object is immutable it can’t be changed in the first place. I may be wrong about this. (I’m now wondering what the implications are for dictionary keys because of this, but that’s a separate topic…))

    As you’ve seen, in some cases the new object may actually be of a mutable class (even if the documentation tells us it won’t). But as long as you don’t rely on it being mutable (why would you?), it doesn’t matter.

    What should you do? Always treat the result of -copy as immutable, simple as that.

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

Sidebar

Related Questions

I'm new to Objective-C and Cocoa. I've read that NSInteger and NSNumber are preferred
I am trying to develop a cocoa application that requires to read highlighted text
I'm still new to Cocoa and don't know much about memory management. I read
I have a 64-bit-only application that I am developing in Objective-C on Cocoa. It
I've read many different things about correct memory management for cocoa/objective-c For instance ive
I've currently been learning Objective-C and Cocoa for some time but I feel that
I'm trying to follow Cocoa and Objective C: Up and Running by OReilly (a
I read somewhere once that the modulus operator is inefficient on small embedded devices
I'm fairly new to Cocoa and Objective-C. Currently I'm developing a fairly basic application
I'm just learning some basic programming in Objective C and Cocoa. I'm trying to

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.