NSMutableArray returning Null from plist



我有一个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)done{ 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我真的不知道如何正确使用它。

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

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

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

该行应为:

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

最新更新