读取plist文件 [英] Reading plist file
问题描述
我创建了一个包含大量不同动物(键)的plist,并将此plist添加到了我的项目中. 每个动物的类型都是NSDictionary.每本字典中还有5个键-这些动物的特征().我想遍历每只动物,如果我符合这种动物的某些特征,我会将它放在另一个阵列中.
I created plist with big base of different animals in it (keys) and added this plist in my project. Type of every animal is NSDictionary. In every dictionary there are 5 more keys - characteristics of these animals (). I would like to go through every animal and if I match a certain characteristics of this animal, I put this animal in another array.
所以,我试图编写一个代码:
So, I tried to make a code:
NSArray *newArray = [[NSArray alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@"plist"]];
for (int i = 0;[newArray objectAtIndex:i];i++)
{
if ([[newArray objectAtIndex:i]objectForKey:@"minAge"] == [NSNumber numberWithInt:3])
{
[array addObject:[[newArray objectAtIndex:i]objectForKey:@"name"]];
}
}
所以objectAtIndex:i
是动物,minAge
是它的特征之一.但是我的代码不起作用.
我是第一次这样做,所以我做错了什么?谢谢!
So objectAtIndex:i
is an animal and minAge
is one of it's characteristics. But my code does not work.
I do it for the first time, so what I have done wrong ? Thanks!
更新: 谢谢,您的plist正常工作!但是我仍然不明白为什么我的plist无法正常工作:
UPDATE: Thank you, your plist works normally ! But I still cant understand why doesn't work mine plist:
无论如何,非常感谢!解决这个问题时,我没有吃饭和睡觉:) 你帮了我很多忙!
Anyway, thank you so much ! I didn't eat and sleep while I was solving this problem :) You helped me a lot !
推荐答案
NSArray 如果无法打开文件或无法将文件内容解析为数组,则返回值为nil
我怀疑问题是您需要将内容放在NSDictionary中,而不是NSArray中.
I suspect the issue is you need to put the contents in a NSDictionary rather than a NSArray
NSDictionary *theDict = [NSDictionary dictionaryWithContentsOfFile:plistFile];
编辑.进一步回答
NSMutableArray *mutableArray =[[NSMutableArray alloc] initWithObjects:nil];
NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@"plist"]];
//--get parent dictionary/Array named animals which holds the animals dictionary items
NSDictionary *parentDictionary = [mainDictionary objectForKey:@"animals"];
//---enumerate through the dictionary objects inside the parentDictionary
NSEnumerator *enumerator = [parentDictionary objectEnumerator];
id value;
while ((value = [enumerator nextObject])) {
if ([[value valueForKey:@"minAge"] isEqualToNumber:[NSNumber numberWithInt:3]])
{
[mutableArray addObject:[value valueForKey:@"name"]];
}
}
如果动物词典不在父词典或数组之内,请使用类似的内容
If the animal dictionaries are not inside of a parent dictionary or Array, use something like this
NSMutableArray *mutableArray =[[NSMutableArray alloc] initWithObjects:nil];
NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"animals" ofType:@"plist"]];
//---enumerate through the dictionary objects
NSEnumerator *enumerator = [mainDictionary objectEnumerator];
id value;
while ((value = [enumerator nextObject])) {
if ([[value valueForKey:@"minAge"] isEqualToNumber:[NSNumber numberWithInt:3]])
{
[mutableArray addObject:[value valueForKey:@"name"]];
}
}
编辑. 1.
尝试使用此plist. 确保使用纯文本编辑器将其粘贴并保存为plist文件
Try it with this plist. Make sure you use a plain text editor to paste it into and save as a plist file
Edit.2. 我现在已经更新了我的列表,使其与您的列表匹配.虽然另一个工作.我觉得使用相同的符号会更有意义.
Edit.2 . I have now updated my plist to match yours. Although the other one worked. I felt it would make more sense that we are using the same.
<?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>Whale</key>
<dict>
<key>name</key>
<string>Whale</string>
<key>isMale</key>
<true/>
<key>color</key>
<string>blue</string>
<key>maxAge</key>
<integer>8</integer>
<key>minAge</key>
<integer>3</integer>
</dict>
<key>Dolphin</key>
<dict>
<key>maxAge</key>
<integer>20</integer>
<key>name</key>
<string>Dolphin</string>
<key>isMale</key>
<false/>
<key>color</key>
<string>blue</string>
<key>minAge</key>
<integer>15</integer>
</dict>
</dict>
</plist>
另外,这是另一个示例,展示了如何将布尔值和数字进行比较
Also, here is another example showing how to compare the boolean and number together
NSMutableArray *theAnimalMatched =[[NSMutableArray alloc] initWithObjects:nil];
NSDictionary *mainDictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data1" ofType:@"plist"]];
//---enumerate through the dictionary objects inside the rootDictionary
NSEnumerator *enumerator = [mainDictionary objectEnumerator];
id returnValue;
while ((returnValue = [enumerator nextObject])) {
if ( [[returnValue valueForKey:@"isMale" ] boolValue] && [[returnValue valueForKey:@"minAge"] isEqualToNumber:[NSNumber numberWithInt:3]] && [[returnValue valueForKey:@"color"] isEqualToString:@"blue"] )
{
[theAnimalMatched addObject:[returnValue valueForKey:@"name"]];
}
}
这篇关于读取plist文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!