如何使用 iPhone 进行 Plist 阵列显示?
我到处寻找示例 Plist 显示代码,但似乎找不到任何可以完成我想做的事情。我知道这不会太难,但我是 Plists 的新手。
我在 Plist 中有以下数组,但似乎无法获得正确的命令来显示信息。我的 Plist 设置有问题吗?它编译得很好,但没有数据到达 iPhone 模拟器。
显示信息的正常方式是什么?
我设置了一个条件:
如果条件一
然后显示“+0+1”
否则
显示“+0+2”
*我希望它显示的是:
*“这是一个”
*“这是两个”
*“这是三个”
* 否则显示
*“555-555-1234”
*“555-555-0000”
*“555-555-5555”
- 我的清单如下
* - 键类型值 *根词典(2项)
- *+0+1 数组(3 项)
- *Item 0 字符串“这是一个”
- *Item 1 字符串“这是两个”
- *Item 2 字符串“这是三个”
- *+0+2 数组(3 项)
- *项目 0 字符串“555-555-1234”
- *项目 1 字符串“555-555-0000”
- *项目 2 字符串“555-555-5555” *
// 将属性列表作为 NSData 对象读入内存
NSData *plistXML = [[NSFileManager defaultManager] contentAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat 格式;
// 将静态属性列表转换为字典对象
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves 格式:&格式 errorDescription:&errorDesc];
//赋值
self.item1 = [temp objectForKey:@"name1"];
self.item2 = [NSMutableArray arrayWithArray:[temp objectForKey:@"name2"]];
// 显示值
bigCheese.text = disp1;
numberZero.text = [disp2 objectAtIndex:0];
numberOne.text = [disp2 objectAtIndex:1];
numberTwo.text = [disp2 objectAtIndex:2];
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"
* "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)
- Key Type Value
- *+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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一旦获得字典,就会获得该词典的所有值。在您的情况下,从临时词典中获取所有值。它应该返回一个包含两个数组的数组。
nsArray *valyeArray = [temp allvalues];
现在您有两个数组,如果要访问“ 555-555-5555”,请这样做:
[[valuesArray objectAtiNdex:1] objectAtIndex:2];
或者,如果您确定“ name1”和“ name2”是词典的键,则可以这样访问它:
[[temp objectofforkey:@“ name1”] objectatiNdex:2];
一旦获得此信息,就可以决定如何显示它。我建议您调试,看看是否首先创建了字典和数组对象。不确定您发布的示例代码中的disp1和disp2是什么。
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:
It's simple, so less chances to do something wrong.