1

我要在 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.
4

2 回答 2

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);
于 2013-04-24T07:43:11.630 回答
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];
于 2013-04-24T07:36:07.057 回答