I am stuck with the next bit of code.
NSString *gridRef = [[NSString alloc] initWithFormat: @"%@", [converter LatLongToOSGrid: latLong]]; NSLog(@"Grid Ref: %@", gridRef); self.answerLabel.text = [[NSString alloc] initWithFormat: @"%@", gridRef];
When I register gridRef, it displays the correct result. However, setting the answerLabel.text line causes an EXC_BAD_ACCESS error, and the program crashes. IB connected to the correct label, what is the problem?
thanks
I updated the code as follows:
- (IBAction)convertLatLong { NSArray *latLong = [[NSArray alloc] initWithObjects: latTextField.text, longTextField.text, nil]; GridRefsConverter *converter = [[GridRefsConverter alloc] init]; NSString *gridRef = [[NSString alloc] initWithFormat: @"%@", [converter LatLongToOSGrid: latLong]]; NSLog(@"Grid Ref: %@", gridRef); NSLog(@"Label: %@", self.answerLabel.text); answerLabel.text = @"Yippy"; self.answerLabel.text = gridRef; [gridRef release]; [converter release]; [latLong release]; }
answerLabel is initialized via @property @synthesize when the view controller is pushed onto the stack. (I donβt know how this happens, besides, this is one of the magical things that IB does for you. Or, I assume that I used the exact same method in other view controllers and did not have this problem.
I found the criminals - the question is, how can I free them?
NSString *eString = [[NSString alloc] initWithFormat: @"%f", e]; NSString *nString = [[NSString alloc] initWithFormat: @"%f", n]; eString = [eString stringByPaddingToLength: (digits/2) withString: @"0" startingAtIndex: 0]; nString = [nString stringByPaddingToLength: (digits/2) withString: @"0" startingAtIndex: 0]; NSString *theGridRef = [letterPair stringByAppendingString: eString]; theGridRef = [theGridRef stringByAppendingString: nString]; [eString release]; [nString release]; return theGridRef;
and
NSArray *gridRef = [[NSArray alloc] init]; gridRef = [gridRef arrayByAddingObject: [NSNumber numberWithDouble: E]]; gridRef = [gridRef arrayByAddingObject: [NSNumber numberWithDouble: N]]; gridRef = [gridRef arrayByAddingObject: [NSNumber numberWithInteger: 8]]; NSString *theGridRef = [[NSString alloc] initWithFormat: @"%@", [self gridRefNumberToLetter: gridRef]]; [gridRef release]; [theGridRef autorelease]; return theGridRef;
}