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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:27:21+00:00 2026-05-31T18:27:21+00:00

I have the following code: .h NSString *mainString; .m case 0: case 1: case

  • 0

I have the following code:

.h

NSString *mainString;

.m

case 0: 
case 1:
case 2:
if ([typeTo textAlignment] == UITextAlignmentRight) {
     typeTo.text = [NSString stringWithFormat:@""];
     mainString = @"";
     [typeTo setTextAlignment:UITextAlignmentLeft];
     typeTo.text = [NSString stringWithFormat:@"%@%d", typeTo.text, [sender tag]];
     mainString = [NSString stringWithFormat:@"%@%d", mainString, [sender tag]];
} else {
     typeTo.text = [NSString stringWithFormat:@"%@%d", typeTo.text, [sender tag]];
     mainString = [NSString stringWithFormat:@"%@%d", mainString, [sender tag]];
}
NSLog(@"%@",mainString);
break;

Crashes on this line usually.

mainString = [NSString stringWithFormat:@"%@%d", mainString, [sender tag]];

Code works one then crashes.
both typeTo.text and mainString start as @""
And text alignment starts left.
What am I doing wrong?

  • 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-31T18:27:23+00:00Added an answer on May 31, 2026 at 6:27 pm

    If you are not using ARC, then you need to either retain the created string or create it with alloc. So either:

    mainString = [[NSString stringWithFormat:@"%@%d", mainString, [sender tag]] retain];
    

    or better yet:

    mainString = [[NSString alloc] initWithFormat:@"%@%d", mainString, [sender tag]];
    

    This of course means you also need to release it before assigning a new value.

    The reason for the crash is likely because you assign the autorelease instance to the pointer, then the object gets autoreleased but the pointer still points to that now-dead object.

    Another way would be to use a property with retain or copy keyword. For strings, copy is usually the better solution because you could accidentally pass a NSMutableString and then later modify it.

    Edit to answer comments:

    In this case, to avoid a memory leak, the following should be done:

    [mainString autorelease];
    mainString = [[NSString alloc] initWithFormat:@"%@%d", mainString, [sender tag]];
    

    The reason why this is necessary is because the mainString is used as an argument to create a new object, which is then in turn assigned to mainString. So before the initWithFormat: line, mainString pointed to a string object A. After that line, it now points to a new string object B. But you need to make sure to clean up A, which is why the autorelease is necessary. If you don’t you’d have a memory leak and eventually your app will run out of memory.

    Alternatively, you could also do:

    NSString *tmp = mainString;
    mainString = [[NSString alloc] initWithFormat:@"%@%d", tmp, [sender tag]];
    [tmp release];
    

    The difference is that autorelease says: I need this object for a short while, but some time after I leave this method it must be cleaned up if possible. release says: I don’t need the object any more, please clean it up now if possible.

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

Sidebar

Related Questions

I have the following code snippet in my Xcode: NSString *digit [[sender titlelabel] text];
I have the following code to open google maps: NSString *urlString = [NSString stringWithFormat:@http://maps.google.com/maps?q=%@,
I have the following code: NSString *text = @http://bit.ly/111 http://bit.ly/222 http://www.www.www; NSRegularExpression *aLinkRegex =
I have the following code: //View guest list -(IBAction) guestList:(id) sender{ NSString *connected =
I have the following code: TTTableItem *item = [TTTableSubtitleItem itemWithText:group.name subtitle:[NSString stringWithFormat:@%@ members %@
I have the following code: NSString *subtitle = [[[node elementsForName:@subtitle] objectAtIndex:0] stringValue]; NSString *duration
How to add news via FBConnect? I have the following code: NSString *newsBody =
I have the following code in place: NSString *mapIDx = @98; NSLog(@map id: %@,
I Have the following code: -(void) changeAnimation:(NSString*)name forTime:(int) times { if(currentAnimation != @attack) {
I have the following code fragment: NSString* value = @value; NSString* key = @key;

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.