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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:04:42+00:00 2026-05-26T11:04:42+00:00

When an NSString object is passed in as an argument, should I always do

  • 0

When an NSString object is passed in as an argument, should I always do retain and release:

-forExample:(NSString*)str{
    [str retain];

    //do something

    [str release];
}

or not? Where and when should I use this?

  • 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-26T11:04:42+00:00Added an answer on May 26, 2026 at 11:04 am

    The reference count of that object isn’t going to change over the course of this method, so there is no reason to send it retain. From Apple’s Memory Management essay (which you should definitely look over):

    If you receive an object from elsewhere in your program, it is normally guaranteed to remain valid within the method or function it was received in. If you want it to remain valid beyond that scope, you should retain or copy it. If you try to release an object that has already been deallocated, your program crashes.

    You only need to retain an object when you need it to stick around past the current scope.

    - (void) forExample: (NSString *)theString {
        // Will need this object later. Stuff it into an ivar and retain it.
        // myString = [theString retain];    
        // For NSString, it's actually better to copy, because this
        // could be a _mutable_ string, and it would in fact be best to use
        // the setter for the ivar, which should deal with the memory management:
        [self setMyString:theString];
    
        // Do things...
    
        // Don't release.
    }
    

    If you have retained an object, you then need to send it release when you no longer need it.

    - (void) otherExample {
        [myString doYourStringThing];
        // If you don't need the string past this point (which would be slightly
        // unusual -- it leaves an ivar unset) release it and set it to nil.
        // [myString release]; myString = nil;
        // Again, it would be best to use the setter:
        [self setMyString:nil];
    }
    
    // Generally you keep ivars around until the instance is deallocated,
    // and release them in dealloc
    - (void) dealloc {
        [myString release];
        [super dealloc];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object which straight forward instance variables. Some are NSString, some are
the following code (from a database object created for each new view): -(void)update:(NSString *)queryText{
dequeueReusableCellWithIdentifier: Returns a reusable table-view cell object located by its identifier. (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier
I have a NSArrayController with following Object: @interface AdressCard : NSObject <NSCoding> { NSString*
I understand that any init... method initializes a new object and that NSString stringWithString
Here's an sample code, where only the string object is released. NSString *nameOfFile =
I need to determine an object's property (passed by name) type in order to
I have a question about reference counting. This is my constructor: - (id)initWithId:(NSString *)graphId;
How to pass a NSString object to stringByAppendingFormat in objective c. I am able
I have a NSString object and want to change it into unichar. int decimal

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.