Re: counting characters
Re: counting characters
- Subject: Re: counting characters
- From: Allan Odgaard <email@hidden>
- Date: Fri, 16 Apr 2004 00:58:26 +0200
On 15. Apr 2004, at 22:24, Jonathan E. Jackel wrote:
If your point is that you can't hand an arbitrary object to the method
and
count stuff, yes, that's true. So what? We have a -count method for
arrays
and a -length method for strings and people don't complain about that.
No, the point was not about name consistency but about writing generic
code as opposed to tackling each new "container" with a new
implementation. I.e. we have one std::count implemented in the
algorithm header -- it works with everything (more or less). What you
provided was an implementation for NSString, making it a category of
NSString did not change the fact that the user need to write the
*implementation* for the algorithm he wishes to use, rather than rely
on a generic algorithm. Moving it to a reusable category does not make
it more general.
If it's important to deal with multiple kinds of objects (which the
original
poster did not mention as a requirement)
My std::count proposal was in reply to "compact", not the OP :)
you can create a generic method that tests self for -isKindOfClass: or
-respondsToSelector: and put it in a category on NSObject. You don't
HAVE to resort to CocoaSTL to be able to reuse your code, although I
can see why that might be useful.
Using isKindOfClass: is *not* generic code, on the contrary. That
said, CocoaSTL is certainly not the only way to write generic code,
although ObjectiveC is a mixture of both primitive and abstract types,
where only the latter make use of dynamic typing, so it is at times
difficult to avoid something like it.
E.g. try to write a function or method which produces a randomly
shuffled version of any Foundation container, including NSString,
NSIndexSet and NSArray.
_______________________________________________
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.