2012-05-31 48 views
0

我有一個NSMutableArray,其中包含從文本字段中獲取的數字列表。NSMutableArray從plist返回NULL

我試圖使用完成按鈕的攻絲來煽動在NSuserdomain內的plist中保存NSMutableArray。要檢查plist的內容,我試圖將它們重新加載到另一個NSMUtableArray中,然後打印數組。

-(NSString *) dataFilePath 
{ NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDirectory = [path objectAtIndex:0]; return  [documentDirectory stringByAppendingPathComponent:@"WeightsList.plist"]; 
} 

- (void)writePlist 
{ 
    [weightsArray writeToFile:[self dataFilePath] atomically:YES]; 
} 

-(void)readPlist 
{ 
    NSString *filePath = [self dataFilePath]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) 
    { 
     NSMutableArray *weightsArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 
    NSLog(@"%@\n",weightsArray); 
    NSLog(@"%@\n", filePath); 
    } 
} 

「完成」 的行動,是在按下完成按鈕所謂:

- (IBAction爲)做 { INT arrayCount;

id arrayVariable; 

arrayCount = [weightsArray count]; 
[weightsArray addObject:weightInput.text]; 
arrayVariable = [weightsArray objectAtIndex:arrayCount]; 
NSLog(@"Weight Entered: %@", arrayVariable); 
NSLog(@"%@",weightsArray); 
[self writePlist]; 
[self readPlist]; 

[self dismissViewControllerAnimated:YES completion:nil]; 

} 

我的問題,是輸出爲每次:

2012-05-31 18:35:05.378 WeighMe[780:f803] /Users/jb/Library/Application Support/iPhone  Simulator/5.1/Applications/BE9D2906-50FA-4850-B3A5-47B454601F61/Documents/WeightsList.plist 
2012-05-31 18:35:05.829 WeighMe[780:f803] Weight Entered: (null) 
2012-05-31 18:35:05.830 WeighMe[780:f803] (null) 
2012-05-31 18:35:05.831 WeighMe[780:f803] (
32432 
) 

這是與plist中無法正常工作的問題,或者是它沒有得到所需數據的NSMutableArray。

我試着用調試器逐步通過運行時,但tbh我真的不知道如何正確使用它。

任何幫助將會很棒!謝謝你們。

回答

1

你重新聲明數組中的 - (空)readPlist方法:

NSMutableArray *weightsArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 

這行應該是:

weightsArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 
+0

好男人!這是解決它的一種享受。這也是一個簡單的錯誤。現在我可以繼續前進,找到另一個問題哈哈。 – Jonnybellman

+1

樂意幫忙! :) – Butaca

相關問題