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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:36:32+00:00 2026-06-18T07:36:32+00:00

NSString * str; ///< Global string. /// Some class method -(void) foo:(NSString*) localStr {

  • 0
NSString * str;  ///< Global string.

/// Some class method
-(void) foo:(NSString*) localStr
{
    str = [[NSString alloc] initWithString:localStr];
}

I’m using ARC – so I can’t manually deallocate the NSString* str if not nil.

I understand that ARC runs in compile-time, so it would not know if to deallocate the NSString before allocating the NSString if foo is called twice.

  • 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-06-18T07:36:34+00:00Added an answer on June 18, 2026 at 7:36 am

    Using ARC, it will automatically dealloc str if it is pointing to a string with no other references and you set it to a new string object. You don’t need to worry about managing the old string when you are setting a new value.

    ARC stands for Automatic Reference Counting and it takes care of calling retain and release for you at the appropriate times (such as when you change the value of str). In fact, you can’t even manually call those functions anymore, but the same things is taking place “behind the scenes”.

    As an aside, you say “I can’t manually deallocate the NSString* str if not nil.”. What you normally do to deallocate an object when using ARC is to just set all of the references to it to nil. So in this case, when you want to deallocate the string (assuming that str is your only reference to it) you simply need to set str to nil: str = nil;. Keep in mind however, as I said above, that you do not need to do this in order to set str to a new value. ARC is smart enough to take care of both at the same time.

    Another aside: You have the following line of code:

    str = [[NSString alloc] initWithString:localStr];
    

    If you actually intended to make a copy, you can replace this with:

    str = [localStr copy];
    

    It is a little shorter and easier to understand. More likely, you don’t need an actual copy and you can just keep a reference to the existing string, so you can use:

    str = localStr;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For NSString, we can use NSString *str = [[NSString alloc] initWithString:@hi] NSString *str =
The code is here: NSString * str = [[NSString alloc] initWithFormat:@abcdefgh]; void * pbuffer
NSString* str = [[NSString alloc] initWithString:@0.05]; NSDecimalNumber* num = [[NSDecimalNumber alloc] initWithString:str]; NSLog(@ %@,
i have a string declare as such NSString *str = [[NSString alloc] initWithFormat:@I require
What's the simplest way, given a string: NSString *str = @Some really really long
- (BOOL)coolMethod:(NSString*)str { //do some stuff Webservice *ws = [[WebService alloc] init]; NSString *result
How to hide @property(retain, nonatomic)NSString*str in some class?
NSString *str = [[NSString alloc]init]; str = [str initWithString:@]; This code crashes. If I
- (NSString *)allocString{ NSString *str = [[NSString alloc] init]; return str; } - (void)viewDidLoad{
I made a method like this: -(void) doSomething:(NSString *)str { } I call it

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.