Solved

ios4 nsstring append question

Posted on 2010-11-22
3
850 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
  • 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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