Solved

ios4 nsstring append question

Posted on 2010-11-22
3
858 Views
Last Modified: 2012-05-10
hello,
i am trying to go through some examples learning xcode and iOS
i am a java guy and pretty new
the problem i am having i think is a memory management or how obj treats string

so i have this method that is called every time a button is pressed.
numberholder is declared as a NSString *numberholder;

1) i press a button with a 9 and it displays a 9 one the console.
2) i press 9 again and on the console is 99 (so far so good)
3) the 3rd time i press 9 it app crashes and goes away

i get no error or anything. i probably have not got to exception handling yet, but this i thought should work

would love a little insight or what concept i am missing,.
thanks
code below

- (IBAction)digitPressed:(UIButton *)sender
{
      NSString *digit = sender.titleLabel.text;
      NSLog(@"digit-->%@\n", digit);      
            if (!numberholder) {
                  numberholder = [NSString stringWithString: digit];
            }else{
                  numberholder = [numberholder stringByAppendingString:digit];
            }
            NSLog(@"NH-->%@\n", numberholder);      
}

0
Comment
Question by:paries
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Expert Comment

by:robertcerny
ID: 34192616
Hello,
declare numberHolder in header as a NSMutableString

NSMutableString *numberHolder;

Open in new window


now in implementation file
 - (id)init
{
 if (self = [super init])
{
         numberHolder = [[NSMutableString alloc] init];
}
return self;

Open in new window


- (IBAction)digitPressed:(UIButton *)sender
{
	NSString *digit = sender.titleLabel.text;
	NSLog(@"digit-->%@\n", digit);      
	
	[numberHolder stringByAppendingString:digit];
 	NSLog(@"NH-->%@\n", numberholder);      
}

Open in new window


Don't forget to release numberHolder in dealloc
0
 

Author Comment

by:paries
ID: 34192810
robertcerny,
thanks for the info
when i do what you suggests
NSLog(@"NH-->%@\n", numberholder); always displays
2010-11-22 16:57:05.845 Calculator[15332:207] digit-->9
2010-11-22 16:57:05.846 Calculator[15332:207] NH-->

so i thought i would try
numberholder = [numberholder stringByAppendingString:digit];

but got the error
/Users/paries/Developer/Calculator2/Classes/CalculatorViewController.m:37:0 /Users/paries/Developer/Calculator2/Classes/CalculatorViewController.m:37: warning: incompatible Objective-C types assigning 'struct NSString *', expected 'struct NSMutableString *'

thanks
0
 
LVL 10

Accepted Solution

by:
robertcerny earned 500 total points
ID: 34194284
Damn,
sorry it was a little bit late for me and my brain was sleeping already ;/
- (IBAction)digitPressed:(UIButton *)sender
{
	NSString *digit = sender.titleLabel.text;
	NSLog(@"digit-->%@\n", digit);      
	
	[numberHolder appendString:digit];
 	NSLog(@"NH-->%@\n", numberholder);      
}

Open in new window

0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface   In the first 100 days of the Macintosh release in January 1984, Apple sold 72,000 of the computers. Relatively cheap, easy-to-use personal computer with a graphical operating system. I've never seen this model. I did see this one:   …
CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question