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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:31:52+00:00 2026-05-15T14:31:52+00:00

I have a situation where I need to use some strings temporarily but I’ve

  • 0

I have a situation where I need to use some strings temporarily but I’ve read so many conflicting things that I’m a bit confused as to what the best way to proceed is.

I need to assign some strings inside an if structure but use them outside the if structure so they need to be created outside the if, I was thinking something like:

NSString *arbString = [[NSString alloc] init];

if(whatever)
{
    arbString = @"Whatever"
}
else
{
    arbString = @"SomethingElse"
}

myLabel.text = arbString;
[arbString release];

I have seen examples where people just used:

NSString *arbString;

to create the string variable

Google’s Objective C guide says it’s preferred to autorelease at creation time:

“When creating new temporary objects, autorelease them on the same line as you create them rather than a separate release later in the same method”:

// AVOID (unless you have a compelling performance reason)
MyController* controller = [[MyController alloc] init];
// ... code here that might return ...
[controller release];

// BETTER
MyController* controller = [[[MyController alloc] init] autorelease];

So I have no idea, which is the best practice?

  • 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-15T14:31:52+00:00Added an answer on May 15, 2026 at 2:31 pm

    In the example you posted, you actually lose the reference to the NSString you created when you assign it in arbString = @"Whatever". You then release the string constant (which is unreleasable, by the way).

    So there’s a memory leak, since you never release the NSString you created.

    Remember that all these types are pointers, so = only reassigns them.

    As for the question, in this example, you don’t need the [[NSString alloc] init]. You don’t need to copy the string into a local variable anyway, you can just set myLabel.text to the string constant @"Whatever".

    (edit: that’s not to say that you can’t use your pointer arbString, arbString = @"Whatever"; myLabel.text = arbString is fine. But this is just pointer assignment, not copying)

    If you needed to manipulate the string before you returned it, you would create an NSMutableString, and either release or auto-release it. Personally, create autoreleased objects using class methods, so in this example, I’d use [NSString string], or [NSString stringWithString:], which return autoreleased objects.

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

Sidebar

Related Questions

i have a situation where i need to use GPS technique. i need to
In my view, if I have a situation where I need to use a
I have a situation where I need to notify some users when something in
I have a situation where I need to create some kind of uniqueness between
I need some help here. Here is my situation: I have a binding list
Here is the situation. I have some ViewModels that contain nested complex model types.
I have Oracle files that I need to compare to CVS files, but the
I have situation where I need to change the order of the columns/adding new
I have a situation where I need to dynamically build up a list of
I have a situation where I need to pass two parameters to an action.

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.