2011-09-20 47 views
0

我已經看到樣品Plist顯示代碼的高和低,似乎無法找到任何我想做的事情。我知道這不是太難,但我對Plists來說是新手。如何用iPhone做Plist陣列顯示?

我在Plist中有以下數組,我似乎無法得到正確的命令來顯示信息。我在Plist設置中做錯了什麼?它編譯好但沒有數據到iPhone模擬器。

顯示信息的正常方式是什麼?

我有一個條件組:

如果條件一個
就會顯示 「+ 0 + 1」
其他
顯示 「+ 0 + 2」


*我是什麼希望它顯示的是:
*「This is one」
*「This is two」
*「This is thre E」
*其他顯示
* 「555-555-1234」
* 「555-555-0000」
* 「555-555-5555」


  • 我PLIST是FOLLOWS
    *
    • 主要類型值 *根詞典(2項)
  • * + 0 + 1門陣列(3項)
  • *項目0字符串 「這是一個」
  • *項目1字符串 「這兩個」
  • *項目2字符串「這三」
  • * + 0 + 2陣列(3項)
  • *項目0字符串 「555-555-1234」
  • *項目1字符串 「555-555-0000」
  • *項目2字符串「555-555-5555 「 *

// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property list into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];

// assign values
self.item1 = [temp objectForKey:@"name1"];
self.item2 = [NSMutableArray arrayWithArray:[temp objectForKey:@"name2"]];
// display values
bigCheese.text = disp1;
numberZero.text = [disp2 objectAtIndex:0];
numberOne.text = [disp2 objectAtIndex:1];
numberTwo.text = [disp2 objectAtIndex:2];

回答

0

嘗試使用:

NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file]; 
NSArray *array = [dict objectForKey:@"Array"]; NSLog(@"%@", array); 

很簡單,所以機會少做錯事。

+0

這看起來像我在尋找一種解決方案。謝謝。 – Ken

+0

我得到的錯誤:你的意思是:[[NSDictionary [NSDictionary? – Ken

+0

對不起,只有一個[,我發錯了 –

0

一旦你得到一本字典,獲取該字典的所有值。在你的情況下,從temp字典中獲取所有值。它應該返回一個包含兩個數組的數組。

的NSArray * valueArray = [溫度allValues];

現在你有兩個數組,如果你想訪問 「555-555-5555」,這樣做:

[valuesArray objectAtIndex:1] objectAtIndex:2]。

或者,如果你確信 「NAME1」 和 「NAME2」 你可以訪問它,這樣你的字典的鍵:

[溫度objectForKey:@ 「名1」] objectAtIndex:2]。

一旦你得到這個信息,然後就由你來決定要如何來顯示它。我建議你進行DEBUG,看看字典和數組對象是否先被正確創建。不知道什麼是您發佈的示例代碼的DISP1和DISP 2 ..

+0

我很欣賞你在這個概念上的擴展。對我來說,我有點困難。我有大約5張桌子,每個桌子大約有60件物品,總共約300件物品。 – Ken