从 plist 读取数据时出现问题

发布于 2024-11-04 02:23:10 字数 1095 浏览 4 评论 0原文

- (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];


}

我已将值保存在 plist 中...现在我想检索它。如果我使用 NSLog 打印它,它会显示 0。我应该如何检索该值?

- (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. If i print that by using NSLog it displays 0. How should I retrieve the value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

平安喜乐 2024-11-11 02:23:10

问题

在您的代码中:

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

path 包含以文档目录为根的文件路径。假设它是 …/plist.plist

NSFileManager *fileManager = [NSFileManager defaultManager];

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

这很奇怪。如果该文件不存在,请将 /plist.plist 附加到 path 变量,该变量将变为 .../plist.plist/plist.plist,这很可能不存在。考虑到这一点,

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

-initWithContentsOfFile:返回nil,因此datanil,所以:

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

不执行任何操作并且in:

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

savedStock 也是 nil,因此 -objectForKey: 后跟 -intValue 返回 0。

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

验证我的假设,使用调试器或NSLog() 用于检查 pathdatasavedStock 的内容。

一种解决方案

如果该文件不存在,则无法读取该文件。因此:

NSFileManager *fileManager = [NSFileManager defaultManager];

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

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

应该是:

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];
}

并且:

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

应该创建包含字典的 …/plist.plist 。并且:

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

应该可以工作,因为在前面的步骤中创建了相应的文件。

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. Let’s say it’s …/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. Considering this,

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

-initWithContentsOfFile: returns nil, so data is nil, so:

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.

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

To validate my assumption, use the debugger or NSLog() to inspect the contents of path, data, and savedStock.

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. 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.

听,心雨的声音 2024-11-11 02:23:10
contentArray = [NSArray arrayWithContentsOfFile:plistPath];

您需要将上述代码放在要从 plist 文件中获取值的位置。您只需要给它您的 plist 路径,它就会将您的 plist 文件转换为数组。然后您可以访问该数组以从 plist 文件中获取数据。

contentArray = [NSArray arrayWithContentsOfFile:plistPath];

you need to place the above code where you want to get values from your plist file. You just need to give it your plist's path and it will convert your plist file into an array. Then you can access this array to get data from your plist file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文