0

我正在开发一个包含多组列表的应用程序。这些将包含许多项目,而这些项目又将包含更多项目。我想在 docs 目录中的 PList 中保存和加载它,但我不确定如何在我正在寻找的层次结构中写入数据。这是层次结构:

>Lists
>>List1
>>>Item1
>>>>Item1Details
>>>Item2
>>>>Item2Details
>>>etc.
>>List2
>>>Item1
>>>>Item1Details
>>etc.

我曾尝试使用 NSDictionary writeToFile,但我仍然不知道如何拥有这样的层次结构。

请给我一些关于如何读/写这种东西的指示。我对这种 plist 的使用完全陌生,所以请多多包涵。

干杯,

哈尔加瓦

4

3 回答 3

2

也许,这会对你有所帮助。

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

    NSString Item1=@"Name";
    NSString Item2=@"Age";
    NSString Item1Detail=@"Khalid";
    NSString Item2Detail=@"21";


        NSMutableDictionary *_list1 = [[NSMutableDictionary alloc] init];
        NSMutableDictionary *_list2 = [[NSMutableDictionary alloc] init];

        [_list1 setValue:Item1Detail forKey:Item1];
        [_list1 setValue:Item2Detail forKey:Item2];

        [_list2 setValue:Item1Detail forKey:Item1];
        [_list2 setValue:Item2Detail forKey:Item2];

        NSMutableArray *_lists = [[NSMutableArray alloc] init];
        [_lists addObject:_list1];
        [_lists addObject:_list2];

        [_list1 release]
        [_list2 release];

        [_lists writeToFile:path atomically:YES];

        [_lists release];
于 2012-02-02T06:45:00.227 回答
0

据我所见,您有对象数组,这是将数组写入 doc 目录中的 plist 文件的代码

    //look at this code which creates path to plist in documents directory:
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
//    >Lists
//      >>List1
//          >>>Item1
//              >>>>Item1Details
//          >>>Item2
//              >>>>Item2Details
//          >>>etc.
//      >>List2
//          >>>Item1
//              >>>>Item1Details
//    >>etc.

    NSArray *list1Arry = [NSArray arrayWithObjects:@"item1Detail",@"item2Detail",@"item3Detail", nil];
    NSArray *list2Arry = [NSArray arrayWithObjects:@"item1Detail",@"item2Detail",@"item3Detail", nil];
    NSArray *list3Arry = [NSArray arrayWithObjects:@"item1Detail",@"item2Detail",@"item3Detail", nil];
    NSArray *lists = [NSArray arrayWithObjects:list1Arry,list2Arry,list3Arry, nil];
    [lists writeToFile: path atomically:YES];
于 2012-02-02T03:10:05.413 回答
0

您可以通过将另一个 NSDictionary 设置为根目录中的值来构建这样的层次结构。所以,这段代码...

NSMutableDictionary *root = [NSMutableDictionary dictionary];
NSMutableDictionary *child1 = [NSMutableDictionary dictionary];
NSMutableDictionary *child2 = [NSMutableDictionary dictionary];

[child1 setValue:@"leaf1" forKey:@"key1"];
[child1 setValue:@"leaf2" forKey:@"key2"];

[child2 setValue:@"leaf3" forKey:@"key3"];
[child2 setValue:@"leaf4" forKey:@"key4"];

[root setValue:child1 forKey:@"child1"];
[root setValue:child2 forKey:@"child2"];

...会产生一个像这样的根对象:

{
   child1: {
      key1: leaf1
      key2: leaf2
   }
   child2: {
      key3: leaf3
      ley4: leaf4
   }
}

可以在 plist 中自动编码的对象类型是NSStringNSDataNSDateNSNumberNSArrayNSDictionary(参见API)。

于 2012-02-02T03:11:43.433 回答