Solved....Check at bottom of Body..
I am using XCODE 4
I have an Money Calculator Application in which I have to press the digit buttons and I have to see it on the TextLabel.
Format like : "$ 500"
So for $ I am appending it..
The method works twice well but the third time it crashes..
Rest if you see the code you will have an Idea.
In my Application I have Three String variables...
-(void)viewDidLoad {
current =[[NSString alloc] retain];
current=[[NSUserDefaults standardUserDefaults] valueForKey:@"Total"]];
NSString *newCurrent=[NSString stringWithFormat:@"$ %@",current];
textViewer.text=newCurrent;
NSLog(@"%@",current);
}
-(IBAction)buttonDigitPressed:(id)sender {
NSString *str= 开发者_运维问答(NSString* )[sender currentTitle];
//NSLog(@"1 The Value Of String %@",str);
NSLog(@"Befor Appending %@",current);
if([current isEqualToString:@"0"])
{
current = str;
}
else
{
current = [current stringByAppendingString:str];
NSLog(@"After Appending %@",current);
}
NSString *newCurrent=@"$ ";
newCurrent = [newCurrent stringByAppendingString:current];
textViewer.text= newCurrent;
} Error is 1st time it is working fine but
when I call it second time, current is Pointing to some Standard Time formatted value, some time it points to some file name value,
Error is: EXC BAD ACCESS
When I am doing it without newCurrent means without appending a letter it works fine.-------SOLVED---------------- Every thing is Ok with My Code I just have to right [current retain]; where ever I am appending the String to the current.
As I was receiving the error EXC BAD ACCESS means I was referring to the Object which was released previously. So, I have retained the Object instead.
Try this code (without memory leaks):
-(void) viewDidLoad
{
current=[[NSString alloc] initWithString:@"20"];
}
-(IBAction)buttonDigitPressed:(id)sender
{
NSString *str= (NSString* )[sender currentTitle];
NSString *nCurrent = [current stringByAppendingString:str];
[current release];
current = [nCurrent retain];
NSString *newCurrent=[[NSString alloc] initWithFormat:@"a%@", current];
textViewer.text= newCurrent;
[newCurrent release];
}
- (void) dealloc
{
[current release];
[super dealloc];
}
no need to alloc or retain the string in both the cases.
in viewdidload
s = [[NSString alloc] init];
s = @"a";
in button click
NSString *newStr = [NSString stringWithFormat:@"b"];
s = [s stringByAppendingString:newStr];
NSLog(@"s is :%@",s);
Hello Arpit,
You can declare property of nsstring in .h file and synthesize it in .m file so i think its help you.
.h file
@property (nonatomic,retain)
NSString *current;
.m file
@synthesize current;
Theres a lot of issues here.
- you have a memory leak in your viewDidLoad when you set current = to a newly allocated object then replace it with @"20"; you've now lost your pointer to the allocated memory. the correct way to do this would be simply to remove the alloc line.
- you have another memory leak when you set newCurrent = [newCurrent stringByAppendingString:current]; for the same reason as 1.
You either need to manage your memory better or just use NSMutableStrings. Change current to be a NSMutableString alloc/init it in your init or viewDidLoad and then just use [current appendString:str];
based on your code you pasted I'd do the following.
in your @interface declare a property
@property (nonatomic, retain) NSString *current;
in your @implementation @synthesize it
@synthesize current;
- (void) viewdidload
{
self.current = @"20";
}
-(IBAction)buttonDigitPressed:(id)sender
{
NSString *str= (NSString* )[sender currentTitle];
self.current = [self.current stringByAppendingString:str];
NSString *newCurrent = @"a";
newCurrent = [newCurrent stringByAppendingString:self.current];
textViewer.text = newCurrent;
}
精彩评论