从plist读取数据时出现问题

时间:2011-04-30 06:30:52

标签: iphone objective-c 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.我应该如何检索值?

2 个答案:

答案 0 :(得分:3)

问题

在您的代码中:

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

没有做任何事情并在:

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

应该有效,因为相应的文件是在前面的步骤中创建的。

答案 1 :(得分:-1)

contentArray = [NSArray arrayWithContentsOfFile:plistPath];

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