2011-02-28 61 views
1

嗨鍵(可可/ iPhone SDK)創建 ,嘗試添加到我的plist編程,繼承人什麼我已經熟了,到目前爲止(對象): 在陣列中的.plist

NSString *documentsDirectory = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents/myfolder"]]; 
     NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Favourites.plist"]; 

     NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:writablePath]; 


     [rootDict setValue:@"My Third PDF" forKey:@"Title"]; 
     [rootDict writeToFile:writablePath atomically: YES]; 

和繼承人我的plist:

<plist version="1.0"> 
<dict> 
    <key>Rows</key> 
    <array> 
     <dict> 
      <key>SaveName</key> 
      <string>first.pdf</string> 
      <key>Title</key> 
      <string>My first PDF</string> 
     </dict> 
     <dict> 
      <key>SaveName</key> 
      <string>second.pdf</string> 
      <key>Title</key> 
      <string>My Second PDF</string> 
     </dict> 
    </array> 
</dict> 
</plist> 

如何去增加一個像

<dict> 
      <key>SaveName</key> 
      <string>third.pdf</string> 
      <key>Title</key> 
      <string>My third PDF</string> 
     </dict> 

我的plist(對象?)?謝謝!

回答

2

你會拳頭必須確保行數組是可變的:

[rootDict setObject:[[rootDict objectForKey:@"Rows"] mutableCopy] forKey:@"Rows"]; 

您將需要創建與結構,例如一個字典

NSDictionary *third = [NDSictionary dictionaryWithObjectsAndKeys: 
         @"third.pdf", @"SaveName", 
         @"My third PDF", @"Title", 
         nil]; 

,並將其添加到陣列:

[[rootDict objectforKey:@"Rows"] addObject:third]; 
+0

優秀的答案謝謝你,我修改了它略低爲* \t的NSMutableDictionary * rootDict = [[的NSMutableDictionary頁頭] initWithContentsOfFile:writablePath]。 \t \t的NSMutableDictionary *第三=的NSMutableDictionary dictionaryWithObjectsAndKeys: \t \t \t \t \t \t \t @ 「third.pdf」,@ 「SAVENAME」, \t \t \t \t \t \t \t @ 「我的第三個PDF」,@ 「標題」 , \t \t \t \t \t \t \t無]; \t \t [[rootDict objectForKey:@「Rows」] addObject:third]; * – DexCurl 2011-03-01 22:43:17