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

  • Home
  • SEARCH
  • 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 6795329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:19:52+00:00 2026-05-26T18:19:52+00:00

by now i was always did the same : NSString *ran = @check; now

  • 0

by now i was always did the same :

 NSString *ran = @"check";

now when i wanted to copy an nsstring1 to nsstring2 , i have been told here that i have to copy/retain the nsstring instance, in a various methods(with property retain/copy) ..

NSString *animation = [ran copy];

i know and understand how to do that , but cant understand why .

what exactly happen with the memory here? i just couldnt understand what happen when i do the copy/retain thing , and when should i do that ? (in the above first exampe also?? )

all explanations here are about the difference between copy/retain , but i couldnt found anything about the REASON , i have to copy a class instance .

  • 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-26T18:19:53+00:00Added an answer on May 26, 2026 at 6:19 pm

    You would copy a string if you need it to be immutable, and you don’t know if it is immutable. Say you have this method:

    - (void)setName:(NSString *)newName
    {
        _name = [newName retain];
    }
    

    You expect me to call your method like this:

    [yourObject setName:@"Rob"];
    

    But I might instead do this:

    NSMutableString *sneaky = [NSMutableString stringWithString:@"Rob"];
    [yourObject setName:sneaky];
    

    and then a while later I might do this:

    [sneaky appendString:@"ert"];
    // sneaky is now @"Robert"
    

    Since your setName: method only retained the string it was passed, I have changed the value of your private _name variable without you knowing about it. Maybe you were depending on it not changing.

    To prevent such sneakiness, you can write your setName: method like this:

    - (void)setName:(NSString *)newName
    {
        _name = [newName copy];
    }
    

    Now you create an immutable copy of whatever name I pass in. If I pass in a mutable string, and then change it, your _name won’t be affected.

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

Sidebar

Related Questions

I have upgraded a SharePoint, excatly same code, but now links to pdfs always
Right now I have this SQL query which is valid but always times out:
i have been using PHP for quite a sometime now, having understood the concept
I have been working with AJAX for a while now, but in limited and
After a few harsh lessons I now always use OrdinalIgnoreCase when comparing Strings in
Im getting frustrated because of OpenDNS and other services (ie: roadrunner) that now always
Right now it always goes to default.aspx that subsequently opens the default controller but
Now I've always heard binary search trees are faster to build from randomly selected
Until now I've always used the ASP.NET MVC framework source for debugging ASP.NET MVC.
Which style of Ruby string quoting do you favour? Up until now I've always

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.