【问题标题】:NSMutableArray returning Null from plistNSMutableArray 从 plist 返回 Null
【发布时间】:2012-05-31 17:47:00
【问题描述】:

我有一个 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 没有获取所需数据。

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

任何帮助都会很棒!谢谢各位。

【问题讨论】:

    标签: xcode null nsmutablearray plist nslog


    【解决方案1】:

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

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

    那一行应该是:

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

    【讨论】:

    • 好人!这是一种享受。也是这么简单的错误。现在我可以继续寻找另一个问题了,哈哈。
    猜你喜欢
    • 2011-12-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多