if wrote the following code and do not understand why the trace returns “false”:
function (with trace):
import mx.controls.Alert;
import mx.collections.ArrayCollection;
public function runTest():void {
var unique1:MyUniqueObject = new MyUniqueObject();
unique1.id = 1;
var unique2:MyUniqueObject = new MyUniqueObject();
unique2.id = 2;
var sameUniqueAsUnique1:MyUniqueObject = new MyUniqueObject();
sameUniqueAsUnique1.id = 1;
var collection:ArrayCollection = new ArrayCollection();
collection.addItem(unique1);
collection.addItem(unique2);
trace(collection.contains(sameUniqueAsUnique1));
}
MyUniqueObject-Class:
package
{
import mx.core.IUID;
[Bindable]
public class MyUniqueObject implements IUID
{
public var id:int;
public function get uid():String {
return "MyUniqueObject." + id;
}
public function set uid(uid:String):void {
//nothing to do here...
}
}
}
Any Ideas?
Cowabunga!
why should it? the implementation uses
==, and actually even===in your case.also, the accessors are never called.
containsis a method ofListCollectionViewand causes the following:ListCollectionView::getItemIndexsort == null && filterFunction == null, thus:IList::getItemIndexFor
ArrayCollectionListCollectionView::listis anArrayListimplementinggetItemIndexasArrayUtil.getItemIndex(item, source);which in fact uses strict comparison between the item and and the source array entries.If you want the call to evaluate to true, you will need to provide your own implementation of
IList. SubclassingArrayListand overridinggetItemIndexwill do the trick although I am not certain it won’t break some other functionality.