Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3: Check if a Dictionary is empty

Flash implements a dictionary (that is, something like a HashMap) using two approaches. One approach is the flash.utils.Dictionary class, and the other is a generic Object. I'd like to check how many key:value pairs are in the dictionary. In most cases I'd simply like to know it there are any key:value pairs, that is, just check if it's empty.

The documentation hasn't been much help on this point. Is there a simple and clear way to do this? Failing that, is there an ugly, yet not too brittle way to do this?

like image 956
Dan Homerick Avatar asked Oct 13 '09 03:10

Dan Homerick


1 Answers

This will reliably tell you if a particular dictionary is empty:

function isEmptyDictionary(dict:Dictionary):Boolean
{
    for each(var obj:Object in dict)
    {
        if(obj != null)
        {
           return false
        }
    }
    return true;
 }

Note that you need to do the obj != null check - even if you set myDictionary[key] = null, it will still iterate as a null object, so a regular for...in loop won't work in that instance. (If you are always using delete myDictionary[key] you should be fine though).

like image 150
Reuben Avatar answered Sep 28 '22 11:09

Reuben