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"]];
}
}
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!
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 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.
I suspect the issue is you need to put the contents in a NSDictionary rather than a NSArray
Edit.Further to my answer
If the animal dictionaries are not inside of a parent dictionary or Array, use something like this
Edit. 1.
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 . 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.
Also, here is another example showing how to compare the boolean and number together
Instead of the following line
try the following