简体   繁体   English

从 plist 读取数据的问题

[英]Problem reading data from a plist

- (void)viewDidLoad {
   [super viewDidLoad];

    paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path]) 
    {
        path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"plist.plist"] ];
    }

    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    int value = 5;
    [data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
    [data writeToFile: path atomically:YES];
    [data release];

    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
    int value1;
    value1 = [[savedStock objectForKey:@"value"] intValue];
    **NSLog(@"%i",value1);**
    [savedStock release];


}

I have saved the value in the plist... now I want to retrieve it.我已将值保存在 plist 中......现在我想检索它。 If i print that by using NSLog it displays 0. How should I retrieve the value?如果我使用 NSLog 打印它会显示 0。我应该如何检索该值?

The Problem问题

In your code:在您的代码中:

paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; 

path contains a file path rooted at the document directory. path包含以文档目录为根的文件路径。 Let's say it's …/plist.plist .假设它是…/plist.plist

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) 
{
    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"plist.plist"] ];
}

This is odd.这很奇怪。 If the file doesn't exist, you append /plist.plist to the path variable, which becomes …/plist.plist/plist.plist , which most likely doesn't exist.如果该文件不存在,则将/plist.plist指向path变量,该变量变为…/plist.plist/plist.plist ,它很可能不存在。 Considering this,考虑到这一点,

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

-initWithContentsOfFile: returns nil , so data is nil , so: -initWithContentsOfFile:返回nil ,所以datanil ,所以:

int value = 5;
[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
[data writeToFile: path atomically:YES];
[data release];

doesn't do anything and in:什么都不做,并且在:

NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
int value1;
value1 = [[savedStock objectForKey:@"value"] intValue];

savedStock is also nil , hence -objectForKey: followed by -intValue returns 0. savedStock也是nil ,因此-objectForKey:后跟-intValue返回 0。

NSLog(@"%i",value1);
[savedStock release];

To validate my assumption, use the debugger or NSLog() to inspect the contents of path , data , and savedStock .要验证我的假设,请使用调试器或NSLog()检查pathdatasavedStock的内容。

One Solution一种解决方案

If the file doesn't exist, you cannot read from it.如果文件不存在,则无法读取。 Hence:因此:

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) 
{
    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"plist.plist"] ];
}

NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

should be:应该:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: path]) 
{
    // If the file exists, read dictionary from file
    data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
}
else
{
    // If the file doesn’t exist, create an empty dictionary
    data = [[NSMutableDictionary alloc] init];
}

and:和:

int value = 5;
[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
[data writeToFile: path atomically:YES];
[data release];

should create …/plist.plist containing the dictionary.应该创建包含字典的…/plist.plist And:和:

NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
int value1;
value1 = [[savedStock objectForKey:@"value"] intValue];
NSLog(@"%i",value1);
[savedStock release];

should work because the corresponding file was created in the previous steps.应该可以工作,因为在前面的步骤中创建了相应的文件。

contentArray = [NSArray arrayWithContentsOfFile:plistPath];

you need to place the above code where you want to get values from your plist file.您需要将上面的代码放在要从 plist 文件中获取值的位置。 You just need to give it your plist's path and it will convert your plist file into an array.你只需要给它你的 plist 的路径,它就会把你的 plist 文件转换成一个数组。 Then you can access this array to get data from your plist file.然后您可以访问此数组以从您的 plist 文件中获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM