13

Is it possible to clear a Flex flash.utils.Dictionary? I have a Dictionary that I want to clear (remove all elements).

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257

6 Answers6

6

I don't believe there is an explicit clear command.

However, you could write your own that would loop through all the keys and run this

delete dict[key]; 

Or you can just reassign

dict = new  Dictionary()
Jason
  • 2,503
  • 3
  • 38
  • 46
6

I think this will work, but I'm not 100% sure, as you're modifying the dictionary while iterating over it:

function clear(d:Dictionary):void {
  for(var id:* in d) {
    delete d[id];
  }
}

However, I generally just create a new Dictionary whenever I need to clear one (though if it's referenced in multiple places then that might not work for you).

Triynko
  • 18,766
  • 21
  • 107
  • 173
Herms
  • 37,540
  • 12
  • 78
  • 101
  • 2
    +1 works. tested it by running through twice, it was empty after first run. – Max Dohme Apr 03 '11 at 01:05
  • 2
    The code above has a defect : 'id' must be of NO type, since a Dictionary can hold any type of object as a key reference and not just a string like dynamic Objects do. The code above would ONLY delete string references! – Bill Kotsias Mar 09 '12 at 17:58
  • I edited the answer to fix the defect. id is now untyped (*), so it can take on any type and clear all possible keys. When it was typed as a string, id would only clear string keys; furthermore, no errors are thrown, and id is not null for non-string keys, they are simply skipped during the iteration. So id must be untyped to iterate over all possible keys. – Triynko Jan 15 '14 at 19:23
2

To remove all the elements in the Dictionary, loop through each key and use the delete keyword

function clear(dict:Dictionary):void
{    
  for(var key:Object in dict) 
  {
        delete dict[key];
  }
}

If you want to be 100% sure that your deleted keys are garbage collected, you must replace your Dictionary instance with a new Dictionary.

dict = new Dictionary();

I once had a Dictionary object with 3 million Strings as keys. Those Strings would only be garbage collected when the Dictionary instance itself was.

greglieb
  • 64
  • 6
1

Not only do you need to make sure the key is of type object or * you need to get the keys first then delete them from the dictionary. Otherwise you will end up one key left in the dictionary(the last one).

I remember the above code working before but recently that has changed with the last iteration of flash player.

This code will ensure they are all removed including the last one.

//Clean up the dictionary
var keys:Array = [];
var key:*;
for ( var key:* in dictionary )
    keys.push( key );
for each ( var key:* in keys )
    delete dictionary[ key ];
0

Just:

dictionary = new Dictionary()
dario
  • 5,149
  • 12
  • 28
  • 32
dscfgos
  • 11
  • 2
0
public static function clear(dict:Dictionary):void {
        var key:*
        for(key in dict) {
            delete dict[key];
        }
    }

Use the above function as a utility function and you can use it throughout your code base.

Rizwan_Khan
  • 313
  • 2
  • 10