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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:23:33+00:00 2026-05-29T13:23:33+00:00

What is the difference between: NSString *string1 = @This is string 1.; and NSString

  • 0

What is the difference between:

NSString *string1 = @"This is string 1.";

and

NSString *string2 = [[NSString alloc]initWithString:@"This is string 2.];

Why am I not allocating and initializing the first string, yet it still works? I thought I was supposed to allocate NSString since it is an object?

In Cocoa Touch,

-(IBAction) clicked: (id)sender{
   NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
   NSString *newLabelText = [[NSString alloc]initWithFormat:@"%@", titleOfButton];
   labelsText.text=newLabelText;
   [newLabelText release];
}

Why do I not allocate and initialize for the titleOfButton string? Does the method I call do that for me?

Also, I’m using XCode 4, but I dislike iOS 5, and such, so I do not use ARC if that matters. Please don’t say I should, I am just here to find out why this is so. Thanks!

  • 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-29T13:23:34+00:00Added an answer on May 29, 2026 at 1:23 pm

    The variable string1 is an NSString string literal. The compiler allocates space for it in your executable file. It is loaded into memory and initialized when your program is run. It lives as long as the app runs. You don’t need to retain or release it.

    The lifespan of variable string2 is as long as you dictate, up to the point when you release its last reference. You allocate space for it, so you’re responsible for cleaning up after it.

    The lifespan of variable titleOfButton is the life of the method -clicked:. That’s because the method -titleForState: returns an autorelease-d NSString. That string will be released automatically, once you leave the scope of the method.

    You don’t need to create newLabelText. That step is redundant and messy. Simply set the labelsText.text property to titleOfButton:

    labelsText.text = titleOfButton;
    

    Why use properties? Because setting this retain property will increase the reference count of titleOfButton by one (that’s why it’s called a retain property), and so the string that is pointed to by titleOfButton will live past the end of -clicked:.

    Another way to think about the use of retain in this example is that labelsText.text is “taking ownership” of the string pointed to by titleOfButton. That string will now last as long as labelsText lives (unless some other variable also takes ownership of the string).

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

Sidebar

Related Questions

NSString *notAllocatedString = @This string was not allocated.; NSString *allocatedString = [[NSString alloc] initWithFormat:@This
What is the difference between these two lines? NSString * string = @My String;
What's the difference between a String Constant and String Literal in plain C? This
Can someone explain the difference between static NSString* CellIdentifier = @Cell; and NSString *CellIdentifier
I think the question is clear enough but still - what's the difference between:
Maybe someone could explain the difference between property: in .h file @property(nonatomic,retain) NSString *someString;
what's the difference between these retains?: @property (nonatomic, retain) NSString *A_StringToRetain; and NSString *B_StringToRetain;
I was just curious, is there any difference between the following two codes? NSString
Possible Duplicate: What is the difference between NSString* mystring and NSString *mystring Should I
Possible Duplicate: Is there a difference between NSString compare: and isEqual(ToString):? What is the

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.