Solved

ios4 nsstring append question

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Download files with device 5 35
iPad and iPhone Application Development 4 557
Apple iphone 3gs 3 387
Need help in phonegap and ipad app test 7 234
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:   …
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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