as3corelib Tutorial:How to Use DictionaryUtil Class in Flex

folder DictionaryUtil class provides an easy way to access the keys collection and values collection of an existing Dictionary object. However, with the source of DictionaryUtil class, I noticed the difference between “for in” and “for each in”.

Search-256x256 Demo | DownloadDownload Full Project


See the following source code pls:

var myObject:Object = {firstName:”Tara”, age:27, city:”San Francisco”};
for (var prop in myObject) {
    trace(”myObject.”+prop+” = “+myObject[prop]);
}
/* output:
myObject.firstName = Tara
myObject.age = 27
myObject.city = San Francisco
*/

var myObject:Object = {firstName:”Tara”, age:27, city:”San Francisco”};
for each (var item in myObject) {
    trace(item);
}
/* output:
Tara
27
San Francisco
*/

However, the “for in” statement just “Iterates over the dynamic properties of an object or elements in an array and executes statement for each property or element.”, “To get a list of fixed properties, use the describeType() function, which is in the flash.utils package.”. From the “Flex Help Document”, we can find the class definition of Dictionary as “public dynamic class Dictionary”. It means that the “key” is defined as a dynamic property of class Dictionary. So, we can get the keys through the “for in” statement.

public static function getKeys(d:Dictionary):Array
{
    var a:Array = new Array();
    for (var key:Object in d)
    {
        a.push(key);
    }
    return a;
}

Screenshot:

DictionaryUtil-01

Click “add to dictionary” to add a new item into the defined Dictionary object.

DictionaryUtil-02

Change the key name to add more items into the object.

DictionaryUtil-03

Methods:

1. getKeys()

public static function getKeys(d:Dictionary):Array

Returns an Array of all keys within the specified dictionary.

Parameters:

d:Dictionary The Dictionary instance whose keys will be returned.

Returns:

Array Array of keys contained within the Dictionary.

2. getValues ()

public static function getValues(d:Dictionary):Array

Returns an Array of all values within the specified dictionary.

Parameters:

d:Dictionary The Dictionary instance whose values will be returned.

Returns:

Array Array of values contained within the Dictionary.

The following is full source code of DictionaryUtilDemo.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
	<mx:Script>
		<![CDATA[
			import mx.collections.ArrayCollection;
			import com.adobe.utils.DictionaryUtil;
			private var dic:Dictionary = new Dictionary();

			[Bindable]
			private var keys:Array;
			[Bindable]
			private var values:Array;

			private function doAddToDictionary():void
			{
				if(dicKey.text == "" )
				{
					var msg:String = "key of dictionary cannot be empty";
					return;
				}

				dic[dicKey.text] = dicValue.text;

				keys = DictionaryUtil.getKeys(dic);
				values = DictionaryUtil.getValues(dic);
			}
		]]>
	</mx:Script>

	<mx:VBox width="100%" height="100%">
		<mx:Form>
			<mx:FormItem label="key">
				<mx:TextInput id="dicKey" text="key1" />
			</mx:FormItem>
			<mx:FormItem label="value">
				<mx:TextInput id="dicValue" text="value1"/>
			</mx:FormItem>
			<mx:FormItem>
				<mx:Button label="add to dictionary" click="doAddToDictionary()"/>
			</mx:FormItem>
		</mx:Form>
		<mx:Text text="keys:{ keys.toString()}"/>
		<mx:Text text="values:{ values.toString()}"/>
	</mx:VBox>
</mx:Application>
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • Reddit
  • Technorati
  • StumbleUpon
  • Twitter
RSS Enjoy this Post? Subscribe to Ntt.cc

RSS Feed   RSS Feed     Email Feed  Email Feed Follow us Follow us
You can leave a response, or trackback from your own site.

4 Responses to “as3corelib Tutorial:How to Use DictionaryUtil Class in Flex”

  1. [...] the art of programming. Here is the summary of the Package com.adobe.utils, include ArrayUtil, DirectoryUtil, DateUtil, IntUtil, NumberFormatter, StringUtil and XMLUtil [...]

  2. James Sun says:

    Very helpful article about the Dictionary class in AS.

  3. An outstanding share! I’ve just forwarded this onto a co-worker who was doing a little homework on this. And he in fact ordered me breakfast simply because I found it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanks for spending the time to discuss this subject here on your blog.

Leave a Reply