• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: counting objects in array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.


  • Prev by Date: Re: Would bindings make sense ?
  • Next by Date: From Carbon to Cocoa
  • Previous by thread: Re: counting objects in array
  • Next by thread: Connecting to a pop server
  • Index(es):
    • Date
    • Thread