Re: counting objects in array
Re: counting objects in array
- Subject: Re: counting objects in array
- From: Philippe Mougin <email@hidden>
- Date: Tue, 20 Jul 2004 21:25:33 +0200
> I came up with the following code to count the occurences of
> objects in an array. In this snippet I count the names of the persons
> in an array (how many Micks, Keiths, Charlies, etc) It works, but
> I was wondering if this is a 'good' approach.
>
> while (person = [enumerator nextObject])
> {
> aName = [person name];
> if ( nil == [[tempDict allKeys] containsObject: aName] ){
> // first occurance
> count = [NSNumber numberWithInt:1];
> }
> else
> {
> count = [NSNumber numberWithInt: ([[tempDict objectForKey:
> aName] intValue] + 1)];
> }
> [tempDict setObject: count forKey: aName];
> }
You could use an NSCountedSet instead:
NSCountedSet *nameCountedSet = [NSCountedSet set];
while (person = [enumerator nextObject])
{
[nameCountedSet addObject:[person name]];
}
See
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/
ObjC_classic/Classes/NSCountedSet.html
Philippe Mougin
F-Script: open source interactive and scripting layer for Cocoa
http://www.fscript.org
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.