Reputation: 2759
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.
Upvotes: 0
Views: 134
Reputation: 101
As you are instantiating your variables locally, they will leak away when you hit the end of the block }.
Try using IVars declared as properties of the particular view controller, synthesized in the .m file.
Look at the C139p at Stanford Course on ITunes, preferably the earlier series given before ARC as this fully explains the concept of data persistence.
Upvotes: 1