Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>NSArray <em>The Return value will be nil if the file can’t be opened or if the contents of the file can’t be parsed into an array.</em></p> <p>I suspect the issue is you need to put the contents in a NSDictionary rather than a NSArray</p> <pre><code> NSDictionary *theDict = [NSDictionary dictionaryWithContentsOfFile:plistFile]; </code></pre> <p>Edit.Further to my answer</p> <pre><code>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"]]; } } </code></pre> <p>If the animal dictionaries are not inside of a parent dictionary or Array, use something like this</p> <pre><code>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"]]; } } </code></pre> <p>Edit. 1.</p> <p>Try it with this plist. Make sure you use a plain text editor to paste it into and save as a plist file</p> <p>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.</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt; &lt;plist version="1.0"&gt; &lt;dict&gt; &lt;key&gt;Whale&lt;/key&gt; &lt;dict&gt; &lt;key&gt;name&lt;/key&gt; &lt;string&gt;Whale&lt;/string&gt; &lt;key&gt;isMale&lt;/key&gt; &lt;true/&gt; &lt;key&gt;color&lt;/key&gt; &lt;string&gt;blue&lt;/string&gt; &lt;key&gt;maxAge&lt;/key&gt; &lt;integer&gt;8&lt;/integer&gt; &lt;key&gt;minAge&lt;/key&gt; &lt;integer&gt;3&lt;/integer&gt; &lt;/dict&gt; &lt;key&gt;Dolphin&lt;/key&gt; &lt;dict&gt; &lt;key&gt;maxAge&lt;/key&gt; &lt;integer&gt;20&lt;/integer&gt; &lt;key&gt;name&lt;/key&gt; &lt;string&gt;Dolphin&lt;/string&gt; &lt;key&gt;isMale&lt;/key&gt; &lt;false/&gt; &lt;key&gt;color&lt;/key&gt; &lt;string&gt;blue&lt;/string&gt; &lt;key&gt;minAge&lt;/key&gt; &lt;integer&gt;15&lt;/integer&gt; &lt;/dict&gt; &lt;/dict&gt; &lt;/plist&gt; </code></pre> <p>Also, here is another example showing how to compare the boolean and number together</p> <pre><code>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] &amp;&amp; [[returnValue valueForKey:@"minAge"] isEqualToNumber:[NSNumber numberWithInt:3]] &amp;&amp; [[returnValue valueForKey:@"color"] isEqualToString:@"blue"] ) { [theAnimalMatched addObject:[returnValue valueForKey:@"name"]]; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload