从 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];
}
我已将值保存在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题
在您的代码中:
path
包含以文档目录为根的文件路径。假设它是…/plist.plist
。这很奇怪。如果该文件不存在,请将
/plist.plist
附加到path
变量,该变量将变为.../plist.plist/plist.plist
,这很可能不存在。考虑到这一点,-initWithContentsOfFile:
返回nil
,因此data
是nil
,所以:不执行任何操作并且in:
savedStock
也是nil
,因此-objectForKey:
后跟-intValue
返回 0。验证我的假设,使用调试器或
NSLog()
用于检查path
、data
和savedStock
的内容。一种解决方案
如果该文件不存在,则无法读取该文件。因此:
应该是:
并且:
应该创建包含字典的
…/plist.plist
。并且:应该可以工作,因为在前面的步骤中创建了相应的文件。
The Problem
In your code:
path
contains a file path rooted at the document directory. Let’s say it’s…/plist.plist
.This is odd. If the file doesn’t exist, you append
/plist.plist
to thepath
variable, which becomes…/plist.plist/plist.plist
, which most likely doesn’t exist. Considering this,-initWithContentsOfFile:
returnsnil
, sodata
isnil
, so:doesn’t do anything and in:
savedStock
is alsonil
, hence-objectForKey:
followed by-intValue
returns 0.To validate my assumption, use the debugger or
NSLog()
to inspect the contents ofpath
,data
, andsavedStock
.One Solution
If the file doesn’t exist, you cannot read from it. Hence:
should be:
and:
should create
…/plist.plist
containing the dictionary. And:should work because the corresponding file was created in the previous steps.
您需要将上述代码放在要从 plist 文件中获取值的位置。您只需要给它您的 plist 路径,它就会将您的 plist 文件转换为数组。然后您可以访问该数组以从 plist 文件中获取数据。
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.