Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

StefanDevil

macrumors member
Original poster
Jul 31, 2010
38
0
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:

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:

Code:
		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

macrumors member
May 24, 2003
75
0
Read the answers to your other question!

You're making the same mistake that was pointed out in your other post.

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
Code:
dictionaryWithContentsOfFile:
class method. Then iterate through the keys that refer to arrays and add together their counts.
 

StefanDevil

macrumors member
Original poster
Jul 31, 2010
38
0
i see i see...thks for the help. finally starting to get a hang of objective c...


i should be using:
Code:
[B]NSDictionary[/B] *array3 = [[[B]NSDictionary dictionaryWithContentsOfFile[/B]: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

Moderator
Staff member
Aug 9, 2009
10,679
8,303
A sea of green
Code:
[B]NSDictionary[/B] *array3 = [[[B]NSDictionary dictionaryWithContentsOfFile[/B]: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/l...ference.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

macrumors member
May 24, 2003
75
0
i see i see...thks for the help. finally starting to get a hang of objective c...


i should be using:
Code:
[B]NSDictionary[/B] *array3 = [[[B]NSDictionary dictionaryWithContentsOfFile[/B]: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 line
Code:
for (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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.