如何调用Document文件夹中的plist文件

时间:2013-04-24 07:33:59

标签: ios objective-c plist

我在Document文件夹中创建plist文件,我想在NSDictionary中调用和存储数据,但我不知道使用什么语法???

我的plist文件是字典类型。

这是我的代码:

#define DOC_DIR [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

     NSString *item = @"Content";
        array = [[NSMutableArray alloc]init];
        for (int i =0; i<8; i++) {
            [array addObject:item];
        }
        NSLog(@"array : %@",array);
        [self WriteDataToPlistFromArray:array];
    - (void)WriteDataToPlistFromArray:(NSArray*)dataArray
    {
        NSDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:dataArray,@"Root", nil];
        NSString *Path = [DOC_DIR stringByAppendingPathComponent:@"mamal.plist"];
        [dic writeToFile:Path atomically:YES];
        NSLog(@"%@",dic);
        NSLog(@"%@",Path);
    }
I want call plist from Document Folder and show data in it.

2 个答案:

答案 0 :(得分:2)

要访问您的plist,请执行以下操作

NSArray *directoryPath = [NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,    NSUserDomainMask ,YES ) objectAtIndex:0];

NSString *filePath = [directoryPath stringByAppendingPathComponent:@"mamal.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"File content : %@",dict);

答案 1 :(得分:1)

DOC_DIR的值应为:

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,    NSUserDomainMask ,YES );
NSString *documentsDir = [paths objectAtIndex:0];

即。您的代码可以重写为:

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,    NSUserDomainMask ,YES );
NSString *documentsDir = [paths objectAtIndex:0];
NSString *Path = [documentsDir stringByAppendingPathComponent:@"mamal.plist"];
[dic writeToFile:Path atomically:YES];