Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Aug 17, 2010, 02:36 AM   #1
StefanDevil
macrumors member
 
Join Date: Jul 2010
plist return count

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.

Last edited by StefanDevil; Aug 17, 2010 at 02:53 AM.
StefanDevil is offline   0 Reply With Quote
Old Aug 17, 2010, 10:04 AM   #2
grimjim
macrumors member
 
Join Date: May 2003
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.
grimjim is offline   0 Reply With Quote
Old Aug 17, 2010, 01:38 PM   #3
StefanDevil
Thread Starter
macrumors member
 
Join Date: Jul 2010
i see i see...thks for the help. finally starting to get a hang of objective c...


i should be using:
Code:
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?
StefanDevil is offline   0 Reply With Quote
Old Aug 17, 2010, 03:57 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by StefanDevil View Post
Code:
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/li...d/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.
chown33 is offline   0 Reply With Quote
Old Aug 18, 2010, 03:50 AM   #5
grimjim
macrumors member
 
Join Date: May 2003
Quote:
Originally Posted by StefanDevil View Post
i see i see...thks for the help. finally starting to get a hang of objective c...


i should be using:
Code:
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 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.
grimjim is offline   0 Reply With Quote
Old Aug 18, 2010, 09:10 AM   #6
StefanDevil
Thread Starter
macrumors member
 
Join Date: Jul 2010
i think i achieved what i want.. thks for the help everyone!
StefanDevil is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Seque Plist? trey5498 iPhone/iPad Programming 16 Feb 18, 2014 04:16 PM
All iPads: Return window, return shipping, other costs? skiltrip iPad 4 Jun 23, 2013 03:24 PM
Annotation in Plist Mvkoe iPhone/iPad Programming 9 Feb 8, 2013 06:12 AM
iPad Mini: Holiday Return Deadline is Jan 7th--keep or return? NateEssex iPad 9 Jan 7, 2013 07:09 PM
PLIST Files esierr1 iPhone/iPad Programming 10 Aug 10, 2012 05:05 PM

Forum Jump

All times are GMT -5. The time now is 05:44 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC