Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ios4 nsstring append question

Posted on 2010-11-22
3
Medium Priority
?
865 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Do you come here a lot? Are you lazy like me and don't want to go through the "trouble" of having to click your Dock's Safari icon and then having to click your Experts Exchange Favorites bookmark to get here? Well then this article is for you.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

783 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