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

Search-256x256Demo | 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值