2012-05-17 97 views
0

我是iPhone开发人员的新手,我正在创建用于阅读ePub文件的ePub阅读器。无法读取/写入数据到.plist文件在iPhone中

我在我的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文件中写入。

任何帮助将被appriciated。

在此先感谢!

回答

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

http://www.mediafire.com/view/?sj8xqadixrl091b chk这是我的文件.. – Krunal

+0

在该文件中:首先,当您应该复制它时,首先尝试从文档文件夹移动文件。但是,您正在尝试移动它而没有实际执行任何操作:没有定义fileManager来完成这项工作。接下来,您将尝试将其移至尚未定义的路径。毫无疑问,你无法打开一个不存在的文件,更不用说写入它了。 – ader

+0

步骤1 :)将文件复制到它的文件夹 – ader

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

还是我的数据是'nil' – Krunal

+0

所以[的NSMutableDictionary的alloc]初始化]零到你的回报?有点奇怪!检查你的代码,因为我在一个测试项目中测试了我的答案,并且它运行良好。 – GreyHands

+0

http://www.mediafire.com/view/?sj8xqadixrl091b chk这 – Krunal

0

这是easer供您使用NSUserDefault,你的数据将被保存到一个plist中文件如下代码:

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

Th恩,你可以通过阅读:

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