On Oct 29, 2011, at 9:06 PM, Kelvin Chung wrote:
> I have this Objective-C container which I want to use in an algorithm that only takes STL iterators. Is there a generic adapter for NSEnumerator or id<NSFastEnumeration> that will provide appropriate STL iterators?
I don't think there is, and there are two alternatives you can choose from to solve this task:
Try to implement a C++ iterator yourself. You may take a look at the boost iterator library: <http://www.boost.org/doc/libs/1_47_0/libs/iterator/doc/index.html>. At least for NSArrays, this seems pretty easy. For NSDictionaries and other containers there is simply no low level API which would be helpful to implement an efficient C++ iterator adaptor and you probably have to use NSEnumerator, which is suboptimal.
The second approach is to use a "block-based enumeration" and provide the algorithm implemented in C++ as a block. (please take a look at "Using Block-Based Enumeration").
> Also, is there a way to tell the difference between two NSEnumerators (a la STL iterator difference)?
You probably meant "distance" - no, there is nothing like this in the Cocoa API. But once you have implemented a proper C++ iterator adapter around a Cocoa container and corresponding iterators, you just call std::distance().
But I can imagine only one use case where this really makes sense: when using NSArrays. In this case, you can evaluate the distance easily using the index.
> Is equality defined for NSEnumerators?
NSEnumerator is an abstract class inherited from NSObject. NSObject conforms to the protocol NSObject, which requires a method isEqual:. So, from this, is Equal: shall be "defined". What it actually returns, sorry, I don't know - it's not documented in those methods which return a concrete instance. In the usual use case, there is no need to send a message isEqual: to instances of NSEnumerator.
> Is it possible to clone an NSEnumerator?
Why want you do that? You may retain it.
Well, please let me note this:
I have the impression that you try to mix Objective-C and C++ at a too fine grained level. You should probably invest some effort to separate them more distinctly.
> 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