我遇到了持久數據的問題。如何使用iphone上的歸檔功能保存數據?
所需的行爲:
我在捕獲用戶輸入的視圖中定義的文本字段。當點擊提交按鈕(在裏面修改)時,我希望將這些數據寫入一個可變數組。到目前爲止,我將在提交時將這個可變數組存檔,但是一旦視圖消失/卸載,也會進行歸檔。當用戶從視圖導航或退出應用程序並重新進入時,我想在文本字段中顯示該可變數組的第一個元素。
實際行爲:
文本字段採集數據和更新myMutableArray在提交。我在視圖上加了一些標籤,加上NSLog來驗證計數隨着我點擊提交而增長。然後我第一次將它存檔,我發現我的文件現在存在於myDocuments中,當我重新訪問視圖並檢查dataArray的數量時,如果該文件存在,則創建數據,dataArray的數量與我創建的元素的數量匹配在myMutableArray中。如果我再次輸入數據,它會重新設置myMutableArray和dataArray。這是我想我遇到的幾個問題之一。
問題:
如果該文件存在,那麼dataArray中被創建。現在我嘗試設置文本框來顯示第一個元素和程序炸彈!如果dataArray存在,我可以看到count是一些正值,但我不明白爲什麼我不能訪問第一個元素。
我覺得當我訪問視圖並通過這個練習時,當我重新訪問視圖時,我看到我有dataArray存在且具有正值,但是當我通過提交按鈕再次添加文本時,我重置了所有內容!所以我可能會堅持一下,但隨後擦乾淨。真正的持久性非常重要。我究竟做錯了什麼?
最後,我將存儲一個有幾個元素的記錄。我將隨時間創建多個記錄供用戶查看。正在歸檔最好的方式?我應該開始使用NSCoding嗎?
- (void)viewDidLoad {
[super viewDidLoad];
// Initialize mutable array and set equal to myMutableArray property
NSMutableArray *aMutableArray = [[NSMutableArray alloc] init];
myMutableArray = aMutableArray;
// Debug Logging
// NSLog(@"Mutable Array Count is: %i", [myMutableArray count]);
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
dataFilePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"data.archive"]];
// Check if the file already exists
if ([filemgr fileExistsAtPath: dataFilePath])
{
NSMutableArray *dataArray;
dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile: dataFilePath];
myTextField.text = @"%@", [dataArray objectAtIndex:0]; // THIS BOMBS OUT MY PROGRAM FOR SURE IF INCLUDED!!!
//myUpdatedMutableArray = dataArray;
// Debug Logging
NSLog(@"Mutable Array (dataArray) Count is: %i", [dataArray count]);
//NSLog(@"The first value in the array is: %i", [dataArray objectAtIndex:0]);
}
[filemgr release];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (IBAction) mySubmitButtonPressed:(id)sender {
// Debug Logging
NSLog(@"Submit Button was Pressed!");
NSLog(@"Mutable Array (myMutableArray) Count is: %i", [myMutableArray count]);
// Create string from textfield and addobject to myMutableArray; check to see that myMutableArray grows in count
NSString *myString = [[NSString alloc] initWithFormat:@"%@",myTextField.text];
[myMutableArray addObject:myString];
NSLog(@"Mutable Array (myMutableArray) Count is: %i", [myMutableArray count]);
// More Debug Logging just using on-screen labels
NSString *mySecondString = [[NSString alloc] initWithFormat:@"%i", [myMutableArray count]];
myFirstLabel.text = myString;
mySecondLabel.text = mySecondString;
// Archive myMutableArray
[NSKeyedArchiver archiveRootObject: myMutableArray toFile:dataFilePath];
//[contactArray release];
}
讓我給這個旋轉。我是一名新秀,所以我正在努力學習,但非常感謝反饋。我會試着清理它,看看我的位置和後退。謝謝。 – newDeveloper 2010-09-06 17:37:04
我已更新我的代碼。現在它更加清潔,我不再覆蓋持久性,它通過用戶會話進行維護。 – newDeveloper 2010-09-07 03:00:18