2012-05-31 42 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