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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:27:09+00:00 2026-05-20T17:27:09+00:00

NSString *s1 = @string1; // NSString *s2 = [[NSString alloc]stringWithFormat:@string2]; NSString *s2 = [[NSString

  • 0
 NSString *s1 = @"string1";
 // NSString *s2 = [[NSString alloc]stringWithFormat:@"string2"];
 NSString *s2 = [[NSString alloc] initWithFormat:@"string2"];

I know this is pretty basic concept but I am not 100% clear with this.

First one doesn’t allocate a memory address and send one allocates a memory address…
Also second one increases the reference count of the variable but not the first one…
Even though I understand the concept but don’t know the situation when I should use the first one or the second one..

When should I use the first one ? also when should use for the second one?

Thanks in advance..

  • 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-20T17:27:10+00:00Added an answer on May 20, 2026 at 5:27 pm

    You should never use the second one – +stringWithFormat: is a class method. Including your first example, you have basically 3 choices:

    NSString *s1 = @"string1";
    NSString *s2 = [[NSString alloc] initWithString:@"string2"];
    NSString *s3 = [NSString stringWithFormat:@"string3"];
    

    s1 in this case is a pointer to a constant string. s2 and s3 both point to new strings that you’ve created, but s2 has been retained for you, and s3 has been autoreleased. If you just need a temporary object, the autoreleased object s3 or the constant object s1 are good choices. If you need to keep the string around, you should use s2 (actually now that I think about it s1 will work in this case too – it’s not really idiomatic, though).

    You can make s1 or s3 equivalent to s2 by sending them a retain message. You could also turn s2 into an equivalent object by sending it an autorelease message.

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

Sidebar

Related Questions

I have this code -(IBAction) generateFlashNow:(id)sender{ [textField resignFirstResponder]; NSString *string1 = textField.text; //NSString *string2
I don't know why this method returns a blank string: - (NSString *)installedGitLocation {
When compiling this: char *str = [[NSString stringWithFormat:@%i days and %i hours, days, hours]
NSString *urlString =[NSString stringWithFormat:@http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d,key1]; NSURL *url = [NSURL URLWithString:urlString]; NSMutableURLRequest *request =
I want to pass a variable to a UIButton action, for example NSString *string=@one;
Here's a piece of code to take a string (either NSString or NSAttributedString) input
When creating a string using the following notation: NSString *foo = @Bar; Does one
Is there a method built in to NSString that tokenizes the string and searches
Consider the 2 following methods of reading a string from a file: NSString *path
If I have an NSString with a text file in it, how do I

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.