Re: Obj-C idioms for list based tasks
Re: Obj-C idioms for list based tasks
- Subject: Re: Obj-C idioms for list based tasks
- From: John Stiles <email@hidden>
- Date: Mon, 07 Apr 2008 09:20:59 -0700
Cocoa doesn't have ordered sets. It has arrays (NSArray), unordered sets
(NSSet), and unordered key-value tables (NSDictionary). Any of these can
optionally be mutable. NSCountedSet also allows for the equivalent of a
"multiset," but for some reason there is no NSCountedDictionary, for the
equivalent of a multimap. In general the Cocoa collection classes aren't
as regular as I would have liked, and don't include all the variants
that an expatriate from C++ or higher-level languages might expect.
So to sort of answer your questions, you probably want:
NSSet setWithArray:, or maybe NSSet setWithObjects:count:
NSSet makeObjectsPerformSelector:withObject:
Paul Sargent wrote:
Coming from other object based languages I'm aware that each language can
have it's own idioms for common tasks. In particular coming from doing a lot
of python, I'm finding myself wanting to do a few things with NSArrays that
I would do quite easily with python lists.
Can anybody suggest a good way to:
1) Given an ordered set of objects, create a new non-mutable ordered set,
with all the duplicates removed?
2) Given an ordered set of objects, create a new non-mutable ordered set,
with each entry being the output of a method applied to each entry of the
original array (e.g. Python map())?
I can easily write 'for each' loops that do these and create mutable arrays,
and then return non-mutable copies, but I half expect that these are common
patterns, and there might be a nice, concise, way of writing them.
Are there any ObjC shortcuts when doing things like this? Am I best writing
a category on top of NSArray the encompass these (and other) patterns? I'd
like to retain ordering, so using NSSet for (1) seems counter productive.
Thanks
P.S. Alternatively, Is there a site like
http://aspn.activestate.com/ASPN/Cookbook/Python/ for Objective C?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden