2012-05-24 88 views
0

我正在嘗試將存儲在NSString變量中的文本保存在與我的項目的主包一起存儲的文本文件中。NSString WriteToFile不保持永久?

到目前爲止,我沒有成功,並嘗試了很多不同的方法。 爲什麼不保持永久?

NSString *pathToFile = [[NSString alloc]init]; 
pathToFile = [[NSBundle mainBundle] pathForResource:@"ListOfSavedImages" ofType:@"txt"]; 
NSLog(@"%@",pathToFile); 

NSString *stringToWriteToFile = [[NSString alloc]init]; 
[email protected]"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); 

實際的文件並不在此代碼段輸出「亞當」的結尾雖然NSLog改變,但我也nslogging文件的內容時的觀點負載,它總是會恢復到原來的文字(它從來沒有真正改變)。我究竟做錯了什麼?

我正在使用Xcode 4.3,ARC和故事板。

回答

1

由於您正在本地實例化變量,因此當您觸碰塊的末尾時,它們將泄漏。

嘗試使用聲明爲特定視圖控制器屬性的IVars,在.m文件中合成。

看看斯坦福大學的C139p課程,最好是ARC之前給出的早期系列,因爲這充分說明了數據持久性的概念。