Cos*_*nus 9 objective-c nsdictionary plist nsarray
我在其中创建了带有大量不同动物的plist(键),并在我的项目中添加了这个plist.每种动物的类型都是NSDictionary.在每个字典中还有5个键 - 这些动物的特征().我想通过每一种动物,如果我匹配这种动物的某些特征,我把这种动物放在另一个阵列中.
所以,我试着制作一个代码:
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"]];
}
}
Run Code Online (Sandbox Code Playgroud)
所以objectAtIndex:i是一个动物,minAge是它的特点之一.但我的代码不起作用.我是第一次这样做,所以我做错了什么?谢谢!
更新:谢谢,你的plist工作正常!但我仍然无法理解为什么我的plist不起作用:

无论如何,非常感谢你!我在解决这个问题的时候没有吃饭和睡觉:)你帮助了我很多!
mar*_*nte 22
NSArray 如果无法打开文件或无法将文件内容解析为数组,则返回值为nil.
我怀疑问题是您需要将内容放在NSDictionary而不是NSArray中
NSDictionary *theDict = [NSDictionary dictionaryWithContentsOfFile:plistFile];
Run Code Online (Sandbox Code Playgroud)
编辑.进一步回答我的问题
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"]];
}
}
Run Code Online (Sandbox Code Playgroud)
如果动物词典不在父词典或数组中,请使用类似的内容
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"]];
}
}
Run Code Online (Sandbox Code Playgroud)
编辑.1.
试试这个plist吧.确保使用纯文本编辑器将其粘贴到并保存为plist文件
编辑.2.我现在更新了我的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>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>
Run Code Online (Sandbox Code Playgroud)
另外,这是另一个示例,显示如何将布尔值和数字进行比较
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"]];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29017 次 |
| 最近记录: |