2012-02-02 80 views
0

我正在研究包含多組列表的應用程序。這些將包含很多項目,其中將包含更多的項目。我想保存並從docs目錄中的PList中加載它,但我不確定如何在我要查找的層次結構中編寫數據。這是層級:將數據作爲層級寫入Plist

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

我一直在使用的NSDictionary將writeToFile試過,但我仍然不知道如何有這樣的層次。

請給我一些關於如何讀/寫這種東西的指針。我對這種使用plists的人來說是全新的,所以請耐心等待。

乾杯,

HBhargava

回答

2

也許,這會幫助你。

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

    NSString [email protected]"Name"; 
    NSString [email protected]"Age"; 
    NSString [email protected]"Khalid"; 
    NSString [email protected]"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]; 
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]; 
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)。