NSMutableArray从plist返回Null

时间:2012-05-31 17:47:01

标签: xcode null nsmutablearray plist nslog

我有一个NSMutableArray,其中包含从文本字段中获取的数字列表。

我试图使用完成按钮的点击来启动将NSMutableArray保存在NSuserdomain中的plist中。要检查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。

我尝试使用调试器逐步运行运行时,但是我真的不知道如何正确使用它。

任何帮助都会很棒!谢谢大家。

1 个答案:

答案 0 :(得分:1)

您正在 - (void)readPlist方法中重新声明数组:

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

那一行应该是:

weightsArray = [[NSMutableArray alloc] initWithContentsOfFile:filePath]; 
相关问题