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):