【问题标题】:Reading plist file读取 plist 文件
【发布时间】:2012-02-04 05:02:43
【问题描述】:

我创建了一个包含不同动物(键)的大型基础的 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"]];                  
      }
}

所以objectAtIndex:i 是一种动物,minAge 是它的特征之一。但是我的代码不起作用。 我第一次这样做,所以我做错了什么?谢谢!

更新: 谢谢,您的 plist 工作正常!但我仍然不明白为什么我的 plist 不起作用:

无论如何,非常感谢!我在解决这个问题时没有吃饭和睡觉:) 你帮了我很多!

【问题讨论】:

    标签: objective-c nsarray plist nsdictionary


    【解决方案1】:

    NSArray 如果文件无法打开或文件内容无法解析成数组,则返回值为 nil。

    我怀疑问题是您需要将内容放在 NSDictionary 而不是 NSArray 中

     NSDictionary *theDict = [NSDictionary dictionaryWithContentsOfFile:plistFile];
    

    Edit.Further to my answer

    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"]];                  
            }
    
    
        }
    

    如果动物字典不在父字典或数组中,请使用类似的东西

    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 文件

    编辑.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>
    

    另外,这里有另一个例子展示了如何比较布尔值和数字

    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"]];                  
        }
    
    
    }
    

    【讨论】:

    • 谢谢markhunte!是的,数组在控制台中返回 nil。我试过你的代码,但它也不起作用。也许我的 plist 文件有问题?
    • 我只是在我的答案中放了一个示例 plist。试试看。
    • 哇,完美运行!太感谢了 !请检查我的主要信息:)
    • 只是为了澄清我的最后评论。你有没有通过没有父字典/数组的代码运行你的 plist
    • 我尝试了这两种代码,当然,如果没有父字典代码,我的应用程序就会崩溃。
    【解决方案2】:

    而不是下面一行

     if ([[newArray objectAtIndex:i]objectForKey:@"minAge"] == [NSNumber numberWithInt:3]) 
    

    试试下面的

     if ( [[[newArray objectAtIndex:i]objectForKey:@"minAge"] isEqualToNumber:[NSNumber numberWithInt:3]]) 
    

    【讨论】:

    • 好的,谢谢!我已经修好了。但不幸的是,它不起作用。我认为我的问题是由其他原因引起的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-24
    • 2011-09-06
    • 2011-01-04
    相关资源
    最近更新 更多