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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:28:15+00:00 2026-05-17T06:28:15+00:00

Whenever I need to create a new NSString variable I always alloc and init

  • 0

Whenever I need to create a new NSString variable I always alloc and init it. It seems that there are times when you don’t want to do this. How do you know when to alloc and init an NSString and when not to?

  • 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-17T06:28:16+00:00Added an answer on May 17, 2026 at 6:28 am

    Whenever I need to create a new NSString variable I always alloc and init it.

    No, that doesn’t make sense.

    The variable exists from the moment the program encounters the point where you declare it:

    NSString *myString;
    

    This variable is not an NSString. It is storage for a pointer to an NSString. That’s what the * indicates: That this variable holds a pointer.

    The NSString object exists only from the moment you create one:

    [[NSString alloc] init];
    

    and the pointer to that object is only in the variable from the moment you assign it there:

    myString = [[NSString alloc] init];
    //Or, initializing the variable in its declaration:
    NSString *myString = [[NSString alloc] init];
    

    Thus, if you’re going to get a string object from somewhere else (e.g., substringWithRange:), you can skip creating a new, empty one, because you’re just going to replace the pointer to the empty string with the pointer to the other one.

    Sometimes you do want to create an empty string; for example, if you’re going to obtain a bunch of strings one at a time (e.g., from an NSScanner) and want to concatenate some or all of them into one big string, you can create an empty mutable string (using alloc and init) and send it appendString: messages to do the concatenations.

    You also need to release any object you create by alloc. This is one of the rules in the Memory Management Programming Guide.

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

Sidebar

Related Questions

I need to create a new entity B whenever I create an entity A
I've been using this pattern whenever I need to create a class that might
Whenever I create a new project in Visual Studio 2010 and don't set the
I have a need to create a HttpSession (via cookie) whenever a client invokes
I need to write a trigger that rounds a value whenever it is inserted/updated
Whenever I want to modify a winform from another thread, I need to use
I need to create an extension of a Flex component, which (obviously) means that
the Trunk of my app points to <IISUrl>http:///localhost/MyApp/</IISUrl> Whenever I create a new branch,
Hi I need to create dojo charts in such a manner that they take
Whenever i need to create SQLiteOpenHelper 'databasehelper' object, i need to pass the context

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.