开发者

How Do You DO A Plist Array Display with iPhone?

I have looked high and low for sample Plist display code and can't seem to find any that does what I want to do. I know it can't be too hard, but I am new to Plists.

I have the following Array in a Plist and I can't seem to get the right command to display the information. Am I doing something wrong in my Plist setup? It compiles fine but no Data gets to the iPhone Simulator.

What would be the normal way of displaying the info?

I have a condition set:

If condition one

then display "+0+1"

else

display "+0+2"


*What I am hoping it to display is:

* "this is one"

* "this is two"

开发者_StackOverflow社区 * "this is three"

* else display

* "555-555-1234"

* "555-555-0000"

* "555-555-5555"


  • MY PLIST IS AS FOLLOWS

    *

    • Key Type Value *Root Dictionary (2 items)
  • *+0+1 Array (3 items)
  • *Item 0 String "this is one"
  • *Item 1 String "this is two"
  • *Item 2 String "this is three"
  • *+0+2 Array (3 items)
  • *Item 0 String "555-555-1234"
  • *Item 1 String "555-555-0000"
  • *Item 2 String "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];


Once you get a dictionary get all values for that dictionary. In your case, get all values from the temp dictionary. It should return you an array containing two arrays.

NSArray *valueArray = [temp allValues];

Now you have two arrays and if you want to access "555-555-5555", do like this:

[[valuesArray objectAtIndex:1] objectAtIndex:2];

Or if you are sure that "name1" and "name2" are the keys of your dictionary you can access it this way:

[[temp objectForKey:@"name1"] objectAtIndex:2];

Once you get this info then it is up to you to decide how you want to display it. I suggest you to DEBUG and see if the dictionary and array objects are created properly first. Not sure what are those disp1 and disp2 in the sample code you posted..


Try using:

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

It's simple, so less chances to do something wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜