Daniel
Daniel

Reputation: 1219

count all items within a dictionary

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:

Any ideas why those counts are this high?

Upvotes: 0

Views: 4291

Answers (6)

Antoine
Antoine

Reputation: 23996

I had to do the same in Swift. I had a Dictionaryof arrays. Resulting in the following code:

var totalItemsOfAllPages:Int {
    var totalItemsCount = 0

    loadedPages.values.array.map({
        totalItemsCount += $0.count
    })

    return totalItemsCount
}

Upvotes: 0

Daniel
Daniel

Reputation: 1219

The solution to my problem is: I've used NSInteger instead of NSUInteger

Upvotes: 0

Daniel
Daniel

Reputation: 1219

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

Upvotes: 0

Alex Reynolds
Alex Reynolds

Reputation: 96984

Use fast enumeration:

NSUInteger count = 0; 
for (id objectKey in alphabetDictionary.allKeys) {
    count += [alphabetDictionary objectForKey:objectKey].count;
}
NSString *countStr = [NSString stringWithFormat:@"Entries (%ud)", count];

Upvotes: 0

Antwan van Houdt
Antwan van Houdt

Reputation: 6991

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

Upvotes: 1

kennytm
kennytm

Reputation: 523734

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];

Upvotes: 3

Related Questions