2014-01-28 60 views
0

如何使用此格式從plist訪問特定的周計劃? 如果有人知道任何有關多級結構化plist的教程,請提出建議。如何訪問多級結構化Plist

下面的代碼給我存儲在plist中

-(void)ReadAppPlist 
{ 
    plistPath = [[NSBundle mainBundle] pathForResource:@"runplan" ofType:@"plist"]; 
    NSMutableDictionary * propertyDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; 
    name = [propertyDict objectForKey:@"Run - 1"]; 

    NSLog(@"%@",name.description); 
} 

plist中是在下面給出的格式的整個數據。

<?xml version="1.0" encoding="UTF-8"?> 
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
    <plist version="1.0"> 
    <dict> 
     <key>Run - 1</key> 
     <dict> 
      <key>Level</key> 
      <string>Beginner</string> 
      <key>Category</key> 
      <string>5K</string> 
      <key>Plan</key> 
      <dict> 
       <key>Duration</key> 
       <string>5week</string> 
       <key>Week 1</key> 
       <dict> 
        <key>Day 1</key> 
        <string>Rest or run/walk</string> 
        <key>Day 2</key> 
        <string>2.5 km run</string> 
        <key>Day 3</key> 
        <string>Rest or run/walk</string> 
        <key>Day 4</key> 
        <string>2.5 km run</string> 
        <key>Day 5</key> 
        <string>Rest</string> 
        <key>Day 6</key> 
        <string>2.5 km run</string> 
        <key>Day 7</key> 
        <string>30 - 60 min walk</string> 
       </dict> 
       <key>Week 2</key> 
       <dict> 
        <key>Day 1</key> 
        <string>Rest or run/walk</string> 
        <key>Day 2</key> 
        <string>3 km run</string> 
        <key>Day 3</key> 
        <string>Rest or run/walk</string> 
        <key>Day 4</key> 
        <string>2.5 km run</string> 
        <key>Day 5</key> 
        <string>Rest</string> 
        <key>Day 6</key> 
        <string>3 km run</string> 
        <key>Day 7</key> 
        <string>35 - 60 min walk</string> 
       </dict> 
      </dict> 
     </dict> 

我不知道如何來檢索這些數據非常和善,如果任何人知道的解決辦法... 在此先感謝,請幫助我。

+0

*運行-1 *有*鍵級別*,一個String(字符串(*初級*)* 5K *)鍵*分類*,鍵*字典*計劃*等 – Larme

+0

是的它具有所有的細節,如級別,類別,計劃。 –

回答

0

通過這種方式,我們可以遍歷我們的plist文件的內部結構......

-(void)ReadAppPlist{ 
     plistPath = [[NSBundle mainBundle] pathForResource:@"runplan" ofType:@"plist"]; 
     NSMutableDictionary * propertyDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath]; 
     //name1 dictionary will have the entire content of plist 
     NSDictionary *name1 = (NSDictionary *)[propertyDict objectForKey:@"Run - 1"]; 
     // here we are traversing to inner structure of elements. 
     NSDictionary *level=[name1 objectForKey:@"Level"]; 
     NSDictionary *category=[name1 objectForKey:@"Category"]; 
     NSDictionary *plan=[name1 objectForKey:@"Plan"]; 
     NSDictionary *week=[plan objectForKey:@"Week 1"]; 
     NSDictionary *Day=[week objectForKey:@"Day 1"]; 

     NSLog(@"LEVEL %@",level); 
     NSLog(@"CATEGORY %@",category); 
     NSLog(@"Week Plan %@",week); 
     NSLog(@"Day Plan %@",Day); 

    } 
2

Using this answer:

NSString *errorDesc = nil; 
NSPropertyListFormat format; 
plistPath = [[NSBundle mainBundle] pathForResource:@"runplan" ofType:@"plist"]; 
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; 
NSMutableDictionary *properties = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistPath mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; 

//Now get the nested dictionary for the key "Run - 1" 
NSDictionary *name = (NSDictionary *)[properties valueForKey:@"Run - 1"]; 

//Now get the nested dictionary for the key "Week 1" 
NSDictionary *name2 = (NSDictionary *)[name valueForKey:@"Week 1"]; 

基本上你可以遍歷層次越來越字典對於valueForKey:

+0

名稱返回整個plist&name2返回空值。 –

+0

我正在使用錯誤的字符串作爲密鑰。我有「第一週」,但你的pList是「第一週」。更新。 – Putz1103

+0

是的,我改變了它,但它仍然返回空值... –