开发者

Why is the uid-getter/setter called (ArrayCollection)?

开发者 https://www.devze.com 2022-12-22 13:49 出处:网络
if wrote the following code and do not understand why the trace returns \"false\": function (with trace):

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);

      开发者_如何学Go      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.

contains is a method of ListCollectionView and causes the following:

  1. call to ListCollectionView::getItemIndex
  2. sort == null && filterFunction == null, thus:
  3. IList::getItemIndex

For ArrayCollection ListCollectionView::list is an ArrayList implementing getItemIndex as ArrayUtil.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. Subclassing ArrayList and overriding getItemIndex will do the trick although I am not certain it won't break some other functionality.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号