views:

3622

answers:

4

When using .contains() on an ArrayCollection in Flex, it will always look at the memory reference. It does not appear to look at an .equals() method or .toString() method or anything overridable. Instead, I need to loop through the ArrayCollection every time and check each individual item until I find what I'm looking for.

Does anyone know why Flex/ActionScript was made this way? Why not provide a way from people to use the contains() method the way they want?

A: 

Couldn't you just extend ArrayCollection and override the contains() method? Alternatively you can paste the source for ArrayCollection into an "mx/collections" package in your project and modify the source; this "monkey-patching technique" will override the behavior throughout your entire project. However I would be extremely cautious about changing ArrayCollection in that manner: since it's used all over the place in the Flex APIs there is a good chance you'll start breaking other components in the framework.

cliff.meyers
A: 

I'm not sure what you mean by "memory reference", but if the contains() method is not doing what you want on the ArrayCollection, would you get your preferred result by invoking contains() directly on the underlying Array? You can do this by accessing the source property of the ArrayCollection.

The documentation for ArrayCollection says this:

The ArrayCollection class is a wrapper class that exposes an Array as a collection that can be accessed and manipulated using the methods and properties of the ICollectionView or IList interfaces.

The key is that ArrayCollection is a wrapper around a regular Array. The wrapper provides additional functionality for the array, and also implements interfaces which are used for making sure the data is in a suitable format when used as a data provider (I presume, I didn't check that from the Flex framework code).

Niko Nyman
A: 

The contains() method searches by reference, correct (I believe even for primitives), so if you're trying to find a string or an int in an ArrayCollection, you'll have to do the searching yourself, by some variation of looping or searching. I don't think any of us could tell you why there isn't, say, an optional parameter on that method indicating whether to search by ref or by val, though; so it goes, as they say.

But I'd definitely warn you off monkey-patching the framework code -- that's just asking for trouble. :)

Christian Nunciato
A: 

Well, it seems like the ArrayCollection doesn't actually look directly at memory, but only as a last resort. It will attempt to find a Unique ID (UID) for the object. If the UID doesn't exist, it will create one for it using the UIDUtil.as.

You can get around this whole default UID stuff by having your object implement the IUID interface and providing your own UID for the object. The ArrayCollection will look at the UID you provide it.

DyreSchlock
Sorry, that's a negative. I don't know what sources you're looking at, but the ones from 3.2 use "==" to find the item if there's a filterFunction applied and ArrayUtil.getItemIndex ("===") if the ArrayCollection is not sorted. If it is sorted it gets more complicated but there's still no UID checking.
bug-a-lot