Header file:
NSString *righta;
@property(nonatomic, retain) NSString *righta;
(I don’t usually make @property declarations for my variables but thought this might help the variable persist through the class, though it hasn’t)
Implementation file:
@synthesize righta;
- (void) function1 {
righta = [NSString stringWithFormat:@"A"];
}
- (IBAction)function2:(id)sender {
NSLog(@"righta is still %@", righta);
}
On trying to access the value of the string in the second function, I receive an “EXC_BAD_ACCESS” and the app crashes.
Any help would be GREATLY appreciated.
Thanks!
stringWithFormatreturns an autoreleased object. You must retain it. Note that you are accessing the ivar directy, not the property so the string is not getting retained. Use the property instead:Some programmers prefer to synthesize their properties with a different ivar name to avoid accessing the ivar directly by mistake.