I am trying to save text stored in an NSString
variable in a text file that is stored with the main bundle of my project.
So far I have had no success and tried a lot of different methods. Why doesn't this stay permanent?
NSString *pathToFile = [[NSString alloc]init];
pathToFile = [[NSBundle mainBundle] pathForResource:@"ListOfSavedImages" ofType:@"txt"];
NSLog(@"%@",pathToFile);
NSString *stringToWriteToFile = [[NSString alloc]init];
stringToWriteToFile=@"Adam";
NSLog(@"%@",stringToWriteToFile);
[stringToWriteToFile writeToFile:pathToFile atomically:YES encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"called!");
NSString *contentsOfFile1 = [NSString stringWithContentsOfFile:pathToFile encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"%@",contentsOfFile1);
The actual file doesn't change although the NSLog
at the end of this code segment outputs "Adam" but I am also nslogging the contents of the file when the view loads and it always reverts back to the original text(it never actually changes). What am I doing wrong?
I am using Xcode 4.3, ARC, and storyboards.