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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:50:55+00:00 2026-05-12T10:50:55+00:00

I am a little curious about the last lines in the two examples presented

  • 0

I am a little curious about the last lines in the two examples presented below (i.e. planetName = [value ??????]) My understanding is that the 1st example with the copy is best as that takes a copy of the string object to protect against the original string object being changed elsewhere.

I am also a little confused by the last line in the 2nd example, again my understanding was that the value object was being passed into the method, I guess I am confused as value is being retained with no associated release? Can someone set me straight?

- (void)setPlanetName:(NSString *)value {
    if (planetName != value) {
        [planetName release];
        planetName = [value copy];
    }
}

.

- (void)setPlanetName:(NSString *)value {
    if (planetName != value) {
        [planetName release];
        planetName = [value retain];
    }
}
  • 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-12T10:50:56+00:00Added an answer on May 12, 2026 at 10:50 am

    Given:

    - (void)setPlanetName:(NSString *)value {
        if (planetName != value) {
            [planetName release];
            planetName = [value copy];
        }
    }
    

    The -copy ensures that if someone passes in an instance of NSMutableString as value, then planetName won’t change in your instances out from under you.

    A good defensive programming pattern. Note that -copy is free on immutable strings; -copy just bumps the retain value and returns self (the instance of the string that was copied).

    Now, consider your second example:

    - (void)setPlanetName:(NSString *)value {
        if (planetName != value) {
            [planetName release];
            planetName = [value retain];
        }
    }
    

    Perfectly valid and works fine, just not as defensive.

    In both cases, planetName must be released in your -dealloc method.

    - (void) dealloc
    {
        [planetName release];
        planetName = nil; // defensive
        [super dealloc];
    }
    

    It doesn’t matter if the string is copied, retained, or was originally a constant string that was passed into your setter. If you retain it (or implied retain it through copy), you must release it.

    Note that you can think of this as “escape patterns”. Whenever the existing value of planetName escapes your object, you must release it. That can happen when the object is deallocated or when a new value of planetName is set, hence the -release in the setters.

    Or, if on Mac OS X, you could turn on garbage collection and be done with it. In any case, you should be using @property & @synthesize to automatically generate the getter/setter pair.

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

Sidebar

Related Questions

I'm a little curious about some of the code that I saw at school
I am new to Objective-C and I am a little curious about how I
I'm workling on a project for my University and im a little curious about
This might sound a little stupid but I am still curious about what the
I have a curious little problem. I have a service that needs to create
I am just curious about the java memory model a little. Here is what
I was curious about defining multiple lexically scoped functions in Scheme that can call
I am designing a software in Python and I was getting little curious about
I know a little about it, was just curious to know more. FileInputStream reads
I'm a little confused about this paragraph on OO visibilty in PHP. was curious

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.