无法在iPhone中读取/写入.plist文件的数据

时间:2012-05-17 09:58:19

标签: iphone ios ipad plist epub

我是iPhone开发人员的新手,我正在创建用于阅读ePub文件的ePub阅读器。

我在我的iphone应用程序中有一个plist,我想读取和写入我的.plist文件中的数据,我面临着问题。

这是我的代码段,

逻辑:首先我要下载ePub文件,.ePub文件将下载到此路径

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSLog(@"basePath=%@",basePath);

输出: -     = /Users/krunal/Library/Application Support/iPhone Simulator/5.1/Applications/6B7FCD58-EDF9-44F4-8B33-5F3542536F92/Documents

现在,我想将已下载.ePub文件的名称写入我的.plist

文件

code:

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

    [data setObject:[NSNumber numberWithInt:value] forKey:@"value"];

    [data writeToFile: plistPath atomically:YES];
    [data release];

我尝试了这个,但我无法写入我的.plist文件。

任何帮助都会受到影响。

提前致谢!!

3 个答案:

答案 0 :(得分:0)

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

数据在这里是零,你应该用:

初始化它
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];

编辑我的回答更清楚:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
if ( basePath == nil ) {
     return
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
NSString *plistPath = [NSString stringWithFormat:@"%@/name.plist", basePath];
[data writeToFile: plistPath atomically:YES];
[data release];

答案 1 :(得分:0)

你的意思是:

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


未经测试的代码如下:

步骤1:将文件复制到其文件夹中。

NSError *error1;
BOOL resourcesAlreadyInDocumentsDirectory;
BOOL copied1;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath1 = [documentsDirectory stringByAppendingString:@"/epub.plist"];

resourcesAlreadyInDocumentsDirectory = [fileManager fileExistsAtPath:filePath1];

if(resourcesAlreadyInDocumentsDirectory == YES) {

} else {

    NSString *path1 = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"/epub.plist"];
    copied1 = [fileManager copyItemAtPath:path1 toPath:filePath1 error:&error1];

    if (!copied1) {
        NSAssert1(0, @"Failed to copy epub.plist. Error %@", [error1 localizedDescription]);
    }
}

第2步:打开它

NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath1]; 

第3步:向其写入数据

[dict setObject:[NSNumber numberWithInt:value] forKey:@"value"];
[dict writeToFile:path atomically:YES];

答案 2 :(得分:0)

您可以更轻松地使用NSUserDefault,您的数据将保存为plist文件,如下代码所示:

- (void)setOverviewGroupInstrument:(BOOL)isGroupded {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:[Your array] forKey:[Your Key]];
[prefs synchronize];
}

然后你可以阅读:

- (NSMutableArray*)getOverviewInstrumentList {
    return [prefs objectForKey:[Your Key]];
}