小小菜鸟的web菜园子

web开发学习。好记性不如烂笔头。每天进步一点点!

导航

as3corelib系列教程之一: DictionaryUtil类的用法

Search-256x256 Demo | DownloadDownload Full Project

See the following source code pls:

  1. var myObject:Object = {firstName:”Tara”, age:27, city:”San Francisco};
  2. for (var prop in myObject) {
  3.     trace(myObject.”+prop+” = “+myObject[prop]);
  4. }
  5. /* output:
  6. myObject.firstName = Tara
  7. myObject.age = 27
  8. myObject.city = San Francisco
  9. */
  10.  
  11. var myObject:Object = {firstName:”Tara”, age:27, city:”San Francisco};
  12. for each (var item in myObject) {
  13.     trace(item);
  14. }
  15. /* output:
  16. Tara
  17. 27
  18. San Francisco
  19. */

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.

  1. public static function getKeys(d:Dictionary):Array
  2. {
  3.     var a:Array = new Array();
  4.     for (var key:Object in d)
  5.     {
  6.         a.push(key);
  7.     }
  8.     return a;
  9. }

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

  1. 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 ()

  1. 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:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  3.     <mx:Script>
  4.         <![CDATA[
  5.             import mx.collections.ArrayCollection;
  6.             import com.adobe.utils.DictionaryUtil;
  7.             private var dic:Dictionary = new Dictionary();
  8.            
  9.             [Bindable]
  10.             private var keys:Array;
  11.             [Bindable]
  12.             private var values:Array;
  13.            
  14.             private function doAddToDictionary():void
  15.             {
  16.                 if(dicKey.text == "" )
  17.                 {
  18.                     var msg:String = "key of dictionary cannot be empty";
  19.                     return;
  20.                 }
  21.                
  22.                 dic[dicKey.text] = dicValue.text;
  23.                
  24.                 keys = DictionaryUtil.getKeys(dic);
  25.                 values = DictionaryUtil.getValues(dic);   
  26.             }
  27.         ]]>
  28.     </mx:Script>
  29.    
  30.     <mx:VBox width="100%" height="100%">
  31.         <mx:Form>
  32.             <mx:FormItem label="key">
  33.                 <mx:TextInput id="dicKey" text="key1" />
  34.             </mx:FormItem>
  35.             <mx:FormItem label="value">
  36.                 <mx:TextInput id="dicValue" text="value1"/>
  37.             </mx:FormItem>
  38.             <mx:FormItem>
  39.                 <mx:Button label="add to dictionary" click="doAddToDictionary()"/>
  40.             </mx:FormItem>
  41.         </mx:Form>
  42.         <mx:Text text="keys:{ keys.toString()}"/>
  43.         <mx:Text text="values:{ values.toString()}"/>
  44.     </mx:VBox>
  45. </mx:Application>

 

 

来自:http://ntt.cc/2008/09/02/as3corelib-tutorial-how-to-use-dictionaryutil-class-in-flex.html

posted on 2008-09-04 17:43  『小小菜鸟』  阅读(1571)  评论(0编辑  收藏  举报