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

  • Home
  • SEARCH
  • 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 536595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:50:19+00:00 2026-05-13T09:50:19+00:00

I have a string (currently defined in my .h file) that I’d like to

  • 0

I have a string (currently defined in my .h file) that I’d like to fill and reuse in my .m file.

Here’s the setup:

  1. User clicks btnA in my interface
  2. btnA runs a method (buttonAClicked) that sets the NSString’s value to “foo”
  3. USer clicks btnB in my interface
  4. btnB runs a method (buttonBClicked) that returns the value of the NSString (eg: “foo”)
  5. User clicks btnA again and the method updates the NSString “a new value”…
  6. Profit!

Here’s some code:

/* Modal_TestAppDelegate.h */
// in the @interface block //
@public
NSString *countOfMatches;
// in the main area of the .h //
@property (nonatomic, readwrite, reatain) NSString *countOfMatches;
/* Modal_TestAppDelegate.m */
@synthesize countOfMatches;
-(void)applicationDidFinishLaunching:(UIApplication *)application{
... other code ...
self.countOfMatches = [[NSString alloc] initWithFormat:@"0"];
}
-(void)updateButtonClicked:(id)sender{
countOfMatches = @"1";
NSLog(@"countOfMatches is now: %@",countOfMatches);
}
-(void)readButtonClicked:(id)sender{
NSLog(@"I wonder what countOfMatches is set to now? %@",countOfMatches); // CRASH!
}

the “readButtonCicked area is where I’m crashing – it looks like I can’t read the countOfMatches string anymore.

Any ideas on how I can simply reuse a “variable” throughout a single class (if I’m calling the .m implementation a “class” correctly – this is my first attempt and I’m kinda ripping pages out of the several Xcode and iPhone SDK books I have).

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-13T09:50:19+00:00Added an answer on May 13, 2026 at 9:50 am

    You should set your NSString property to copy, not retain. (More here)

    @property (nonatomic, readwrite, copy) NSString *countOfMatches;
    

    You’re also leaking memory on this line

    self.countOfMatches = [[NSString alloc] initWithFormat:@"0"];
    

    It could be

    self.countOfMatches = [[[NSString alloc] initWithFormat:@"0"] autorelease];
    

    or even better:

    self.countOfMatches = [NSString stringWithFormat:@"0"];
    

    or even best (and really, what it should be):

    self.countOfMatches = @"0";
    

    There’s no sense using any of the “format” methods of NSString — you’re just setting it to a static string.

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

Sidebar

Related Questions

No related questions found

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.