PDA

View Full Version : plist return count




StefanDevil
Aug 17, 2010, 01:36 AM
hi, for plist of the form:

Root-->

ABC---> item1
---> item2

XYZ---> item1
---> item2
---> item3
....

how can i get the count of item 1,2,3,4 etc. as in for ABC, it will return 2, for XYZ it will return 3.

THks

i use the following code:


NSArray *arr = [NSArray arrayWithContentsOfFile:finalPath];
int count = 0;
for (NSDictionary *dict in arr) {
if([myString isEqualToString:[array3 objectForKey:myString]])
count += dict.count;
}



where myString is what user enter, it will go to the database with the matching key.

but when i use the following code only:


NSArray *arr = [NSArray arrayWithContentsOfFile:finalPath];
int count = 0;
for (NSDictionary *dict in arr) {
count += dict.count;
}



my count value is always = 0. which is not what i want.



grimjim
Aug 17, 2010, 09:04 AM
You're making the same mistake that was pointed out in your other post (http://forums.macrumors.com/showthread.php?t=994082).

You have a plist, which is effectively a representation of an NSDictionary object. It may contain arrays of other values, but it is not itself an array of NSDictionary objects. So when you try to initialise your NSArray object with the contents of the file, it fails, and returns nil. But since you aren't checking for that condition, you simply carry on and are surprised when your count variable doesn't get incremented.

Try creating an NSDictionary object using its dictionaryWithContentsOfFile: class method. Then iterate through the keys that refer to arrays and add together their counts.

StefanDevil
Aug 17, 2010, 12:38 PM
i see i see...thks for the help. finally starting to get a hang of objective c...


i should be using:

NSDictionary *array3 = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
int count = 0;
for (NSDictionary *dict in array3){
count += dict.count;
}


but this way count will be equal to total number of keys or the total number of items of all the keys?

chown33
Aug 17, 2010, 02:57 PM
NSDictionary *array3 = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
int count = 0;
for (NSDictionary *dict in array3){
count += dict.count;
}


but this way count will be equal to total number of keys or the total number of items of all the keys?

Try it. See what happens.

This is often the simplest way to answer a hypothetical question involving code.

I suspect it won't work. The type of array3 is NSDictionary, even though the variable name indicates an array. The fast-enumeration for NSDictionary enumerates keys, not values. Reference doc:
http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/doc/uid/20000140-SW2


You still seem to be confused about the difference between NSDictionary and NSArray. I suggest that you try smaller simpler test programs so you can test yourself to see if your understanding is correct or not.

grimjim
Aug 18, 2010, 02:50 AM
i see i see...thks for the help. finally starting to get a hang of objective c...


i should be using:

NSDictionary *array3 = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
int count = 0;
for (NSDictionary *dict in array3){
count += dict.count;
}


but this way count will be equal to total number of keys or the total number of items of all the keys?

Nope.

chown33 is right: you need to go back and read the documentation for NSDictionary. You seem to have lost track of what you're actually doing. The linefor (NSDictionary *dict in array3){ for example suggests to me that you haven't fully understood the changes you've just made to your code. Maybe changing the variable name from array3 to, say, dict3, will make it more obvious. NSDictionaries are not the same as NSArrays. (chown33 also gave you a valuable clue by telling you that fast enumeration on NSDictionaries works on the keys, not the values. That should help you understand the runtime error you will get when you try and run your code. ;))

Just so that I'm clear with what you're trying to achieve here; I'm assuming that you have some plists which contain arrays of objects, and you want to iterate through each array in the plist and compare it against a given string, and then count the number of matches. Is that correct? If so, I suggest that you go back and re-read the documentation for NSDictionary and NSArray and make sure that you understand what the differences are. (A clue: in the first instance, I would take a look at NSDictionary's allKeys: and allValues: methods. These might be very useful, depending on what you are trying to achieve.) Another point to consider is what is included in your plist. If there are a couple of arrays that you're interested in, plus a bunch of other entries that you don't need to search through, you only need to pull out the entries you're searching through. Easy enough to do if you know their keys.

StefanDevil
Aug 18, 2010, 08:10 AM
i think i achieved what i want.. thks for the help everyone! :)