0

我是 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文件。

任何帮助将不胜感激。

提前致谢 !!

4

3 回答 3

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]];
}
于 2012-05-17T11:12:09.657 回答
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];
于 2012-05-17T10:07:29.003 回答
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]);
    }
}

第二步:打开

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

第 3 步:向其写入数据

[dict setObject:[NSNumber numberWithInt:value] forKey:@"value"];
[dict writeToFile:path atomically:YES];
于 2012-05-17T10:27:34.407 回答