I have a .h file where 2 variables are declared, then I have a .m file with a variety of methods, all of which need to access the 2 variables from the .h file.
Here is the .h file, with the 2 variables paths and documentsDirectory
@interface create_textViewController : UIViewController {
IBOutlet UITextField *textField1;
IBOutlet UITextView *textView;
NSArray *paths;
NSString *documentsDirectory;
}
In my .m file, the first method is supposed to initialize the 2 variables, and then the other method is just meant to access them.
-(void)viewDidLoad{
[super loadView];
NSLog(@"viewdidload");
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
}
-(void)toiPad{
NSString *name = [NSString stringWithFormat:@"%@/output.txt", documentsDirectory];
NSString *content = [[NSString alloc] initWithFormat:@"%@", textView.text];
[content writeToFile:name atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
}
However, when i put in an NSLog in the toiPad method and try to output the documentsDirectory, the program crashes.
Any advice? I think I may just been missing something obvious.
I would imagine (the reference doesn’t explicitly state it) that
NSSearchPathForDirectoriesInDomainsreturns an autoreleased object, so try:You’ll need to release it in the
deallocmethod, which I assume you’re already doing.Don’t bother keeping the
pathsvalue at all (just callNSSearchPathForDirectoriesInDomainswhenever you need it again).