views:

188

answers:

4

Hi guys,

my question is: I have a dictionary with one object for each character of the alphabet. Within those objects, there are all values for a specific character.

Example:

alphabetDictionary
  a
    apple
    alien
  b 
   balloon
   ball

I now want to count all entries within this dictionary:

apple
alien
balloon
ball
-> 4

Using this sourcecode only counts the objects (a, b, etc)

NSString *counter = [NSString stringWithFormat:@"Entries (%d)", alphabetDictionary.count];

So how do I get all entries and not the objects a,b,etc?

EDIT 1: In the viewDidLoad Method I have:

//Initialize the array.
charDict = [[NSMutableDictionary alloc] init];
alphabetArray = [[NSMutableArray alloc] init];

if ([charSpecialArray count] > 0)   {

    //Set object for this character within the dictionary
    [charDict setObject:charSpecialArray forKey:@"#"];
    //fill the alphabetArray with an index value, if this char exists at all
    [alphabetArray addObject:@"#"];
}

if ([charAArray count] > 0) {

    [charDict setObject:charAArray forKey:@"A"];
    [alphabetArray addObject:@"A"];
}

etc.. for each character

followed by:

totalCounter = 0;
//Count all keys within the Dictionary
for (id objectKey in charDict.allKeys) {
    totalCounter += [[charDict objectForKey:objectKey] count];
    NSLog(@"Anzahl: %d", totalCounter);
}   
NSString *counter = [NSString stringWithFormat:@"TXNs (%d)", totalCounter];

self.title = counter;

the results are looking much too high:

  • Character H has 33 entries
  • the count of H characters results in: 132
  • m characters: 1587
  • m characters count: 6348

Any ideas why those counts are this high?

A: 

Use fast enumeration:

NSUInteger count = 0; 
for (id objectKey in alphabetDictionary.allKeys) {
    count += [alphabetDictionary objectForKey:objectKey].count;
}
NSString *countStr = [NSString stringWithFormat:@"Entries (%ud)", count];
Alex Reynolds
Well.. the counting itself works.. but anyhow the number I get is much too high..I've updated my question...
Daniel
I'm not sure what would be wrong with code, honestly. I would do test cases with a dictionary that has a small set of known objects in it.
Alex Reynolds
the problem was I used NSInteger instead if NSUInteger
Daniel
Daniel, if you solved the problem, please do one of the following: a) post an answer of your own, and select it b) select someone else's answer, c) delete the question (if, in the end, you think the solution was trivial, and the question is not relevant to anyone else)
Felixyz
+1  A: 

a, b, etc. are keys. The arrays [apple, alien], etc. are objects.

To get the total count, you need to loop through the dictionary and sum it up:

NSUInteger total = 0;
for (NSString* key in alphabetDictionary) {
  total += [[alphabetDictionary objectForKey:key] count];
}
return total;

Or use this very slow one-liner:

int count = [[[d allValues] valueForKeyPath:"@sum.@count"] intValue];
KennyTM
unfortunately there remains an error with very high numbers of the count...
Daniel
A: 
NSEnumerator *aEnum = [alphabetDictionary objectEnumerator];
id object;
NSUInteger count = 0;
while( object = [aEnum nextObject] ){
 count += [object count]; // assuming that the object is an array
}
NSString *countStr = [NSString stringWithFormat:@"Entries (%ld)",count];
// %d NO ofcourse not its not a NSInteger we want a Long
Antwan van Houdt
unfortunately this doesn't fix the high number problem, too..
Daniel
A: 

I'm not able to delete this question because of the votings and all the answers. So here is my own answer to the question why the number of counted entries where so high:

I used NSInteger instead if NSUInteger

Daniel