Solved

ios4 nsstring append question

Posted on 2010-11-22
3
847 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now