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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:38:12+00:00 2026-05-22T01:38:12+00:00

If I set a NSString as property @property (copy) NSString* name; I always want

  • 0

If I set a NSString as property

@property (copy) NSString* name;

I always want to use (copy), so that if its value change, all object with such string still have the old value (as specifiedhere).

However, what happens if my NSString is not a class property, but it is just declared in the code ? Is in that case retained or copied everytime I assign a new value ?

thanks

  • 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-22T01:38:12+00:00Added an answer on May 22, 2026 at 1:38 am

    It depends on how you declare it. You should read the documentation on memory management. Basically the rules are:

    NSString *aString = [NSString stringWithString:@"Hello"];
    NSString *bString = [NSString stringWithFormat:@"%@", @"Hello"];
    

    In these cases, the string is not copied or retained. It is autoreleased, which means it will be automatically deallocated the next time the autorelease pool drains. You do not have to call the release method on them. (So assigning a new value will not cause it to leak.)

    NSString *cString = [[NSString alloc] initWithFormat:@"%@", @"Hello"];
    [cString release];
    

    According to Objective C convention, methods that use alloc and have a retain count of one are not autoreleased, so you need to release them explicitly. Assigning a new value without releasing the old one will cause a leak.

    You can also explicitly call a “copy” method or a “retain” method on a string. In either case, the new string will have a retain count of 1 and will not be autoreleased, so you will need to call the release method on it before you assign a new value.

    NSString *dString = [cString retain];
    NSString *eString = [cString copy];
    
    ...
    [dString release];
    [eString release];
    

    If it is a property, and you use self.variableName, this will be taken care of for you (through the getters and setters which are generated with @synthesize). If you do it explicitly, you must make sure to call release on variables that you have called retain or copy on.

    Edit: As some commentators below have noted, thinking about management in terms of “ownership” is usually the preferred of describing these ideas, rather than retain count.

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

Sidebar

Related Questions

I see many discussions saying that I should use copy for NSString property because
I set a value in one class and then I want to retrieve that
I want to set an NSString property of a main view from a nested
I've got a Core Data property that I'm trying to set at runtime, with
I have a UILabel called optionsDisplayerBrushSizeLabel who's text property I want to set to
//viewController.h file //--------------------- #import <UIKit/UIKit.h> @interface ItemClass : NSObject { NSString* name; } @property
I am try to use stringWithFormat to set a numerical value on the text
I have a class NSFoo that has a bar property. I want to have
I have the following problem: in header; GDataXMLDocument *doc; NSString *xmlBody; @property (nonatomic,copy) NSString
I have the following object: @interface SomeObject : NSObject { NSString *title; } @property

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.