2010-09-06 40 views
0

我遇到了持久數據的問題。如何使用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]; 

} 

回答

1

這個代碼有很多問題。

首先,你泄漏內存到處都是。具體位置:

NSMutableArray *aMutableArray = [[NSMutableArray alloc] init]; 
myMutableArray = aMutableArray; 

這確實應該是:

self.myMutableArray = [NSMutableArray array]; 

您取消歸檔數據後,如果您想保留它在myMutableArray,只要做到這一點:

[myMutableArray appendArray: dataArray]; 

或者只是:

self.myMutableArray = dataArray; 

你不應該像NSFileManager一樣釋放單例實例,所以完全擺脫這個:

[fileMgr release];

你的代碼崩潰,因爲這是不合法的代碼:

myTextField.text = @"%@", [dataArray objectAtIndex:0]; 

(它實際上是合法的代碼,但它會做非常不同的東西比你想象的)

這不是Python的,所以你必須做到這一點:

myTextField.text = [NSString stringWithFormat: @"%@", 
    [dataArry objectAtIndex: 0]]; 

但是,這確實是一樣的:

myTextField.text = [dataArray objectAtIndex: 0]; 

mySubmitButtonPressed中的數組中添加一個字符串比您想象的要簡單得多。只是這樣做:

[myMutableArray addObject: myTextField.text]; 

你真的應該瞭解自動釋放對象,內存管理什麼保持性實際上做。

+0

讓我給這個旋轉。我是一名新秀,所以我正在努力學習,但非常感謝反饋。我會試着清理它,看看我的位置和後退。謝謝。 – newDeveloper 2010-09-06 17:37:04

+0

我已更新我的代碼。現在它更加清潔,我不再覆蓋持久性,它通過用戶會話進行維護。 – newDeveloper 2010-09-07 03:00:18