【问题标题】:unable to read/write data to .plist file in iPhone无法读取/写入数据到 iPhone 中的 .plist 文件
【发布时间】:2012-05-17 09:58:19
【问题描述】:

我是 iPhone 开发者的新手,我正在创建 ePub 阅读器来阅读 ePub 文件。

我的 iphone 应用程序中有 plist,我想在我的 .plist 文件中读取和写入数据,但我在其中遇到了问题。

这是我的代码 sn-p,

逻辑:首先我下载一个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

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

code:

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

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

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

我试过这个,但我无法在我的.plist 文件中写入。

我们将不胜感激。

提前致谢!

【问题讨论】:

    标签: iphone ios ipad plist epub


    【解决方案1】:

    你的意思是:

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

    【讨论】:

    • 在该文件中:首先,当您应该复制文件时,您正在尝试从文档文件夹中移动文件。但是,您试图在没有实际做任何事情的情况下移动它:没有定义 fileManager 来完成这项工作。接下来,您尝试将其移动到尚未定义的路径。难怪你不能打开一个不存在的文件,更不用说写了。
    • 步骤 1:) 将文件复制到它的文件夹
    • 你能建议一些代码 sn-p,我对 iPad 很陌生。
    • 我试过你的编辑,谢谢你的帮助,但我仍然在 SIGABRT if(!copied1) 条件pastebin.com/N3tHVHWD
    【解决方案2】:
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: basePath];
    

    data 此处为 nil,您应该使用以下命令对其进行初始化:

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

    【讨论】:

    • 所以 [[NSMutableDictionary alloc] init] 返回 nil 给你?有点奇怪!检查您的代码,因为我在一个测试项目中测试了我的答案并且效果很好。
    • 我认为错误的部分是你保存一个 NSDictionary 然后加载一个 NSArray。如果您想要一个数组,请创建一个 NSDictionary 的 NSArray 并保存它。
    • 你能建议一些代码 sn-p,我对 iPad 很陌生。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      相关资源
      最近更新 更多