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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:48:59+00:00 2026-05-11T16:48:59+00:00

I have a @property which is defined like this : @property (nonatomic, retain) name;

  • 0

I have a @property which is defined like this :

@property (nonatomic, retain) name;

In a function I parse some xml and set the name property.
My question is should I explicitly release previous retained instance before retain a new one ?

For exemple :

myObj.name = [otherObj getName]; // retain count +1

..

myObj.name = [otherObj getName]; // which is a new instance of a name, is the previous name released ?

  • 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-11T16:48:59+00:00Added an answer on May 11, 2026 at 4:48 pm

    In a synthesized property setter, the code does something roughly analogous to this (we’ll have name be the property name, and _name will be the instance variable to which it refers):

    - (void) setName: (NSString *) newValue
    {
      if ( newValue == _name )
        return;
    
      [newValue retain];
      [_name release];
      _name = newValue;
    }

    Now, that shows what would happen based on your property declaration above. Depending on the other possible attributes of that property, that function would either use [newValue copy] instead of [newValue retain] (if the ‘copy’ attribute was specified), or it would all be wrapped in an @synchronized(self) block (if the ‘nonatomic’ attribute was NOT supplied).

    I should also note that since your property refers to an NSString, which implements the NSCopying protocol, you should really be using copy rather than retain, i.e.:

    @property (nonatomic, copy) NSString * name;
    

    That will cause the synthesized setter to use -copy instead of -retain, which is safer when you’re actually passed an NSMutableString object. An immutable string would ultimately only get retained by the -copy call, while a mutable string would create a new immutable copy. Failing to do that means that the string itself could be changed after it’s been passed into the setter, changing the value without your object’s knowledge.

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

Sidebar

Related Questions

In my application I have a class which has properties of user-defined types like
In C# 3.0, I have a property which is suppose to contain the version
I have two collections which have property Email in both collections. I need to
I have a base class with a property which (the get method) I want
I have an object with a NameValueCollection property which I'm managing with Linq2SQL. I
I have a request that returns a JSON object with a single property which
I have a List of beans, each of which has a property which itself
I have a collection of custom entity objects one property of which is an
Does anyone know which property sets the text color for disabled control? I have
I have a c# object with a property called Gender which is declared as

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.