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 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

Ask A Question

Stats

  • Questions 268k
  • Answers 268k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer adding it to the mouseover will not help, as the… May 13, 2026 at 1:03 pm
  • Editorial Team
    Editorial Team added an answer This sounds exactly like what the sub repository support in… May 13, 2026 at 1:03 pm
  • Editorial Team
    Editorial Team added an answer Byte compiled files load up faster so I'd recommend that… May 13, 2026 at 1:03 pm

Related Questions

I am a little curious about the cute little kaleidoscopic images associated with each
I am new to Objective-C and I am a little curious about how I
I am a little curious to know about how OpenID authentication works. Is there
I am just curious about the java memory model a little. Here is what

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.