2011-06-28 65 views
7

ActionScript 3的一對夫婦對Dictionary類問題:AS3字典問題

  1. 什麼檢查未使用的鍵的最佳方式?我現在在做dictionary[key] == undefined。這是最快和最乾淨的方式?

  2. 我必須循環和delete dictionary[key]或者我可以讓字典超出範圍嗎?

  3. 有沒有更好的解決方案將消息監聽器映射到廣播級?我做這樣的事情:

    addListener(function : Function, messageType : Type) 
    { 
        if(dictionary[messageType] == undefined) 
         dictionary[messageType] = new Vector<Function>(); 
    
        dictionary[messageType].push(function); 
    } 
    
    broadcast(message : Message, messageType : Type) 
    { 
        if(dictionary[messageType] != undefined) 
        { 
         for each(var function : Function in dictionary[messageType]) 
          function(message); 
        } 
    } 
    

我剛剛輸入了這一點,現在,所以它可能不是100%準確。對於像這樣的字典使用路由系統是個好主意嗎?

+0

對於N°1另外還有'in'操作:'如果(在字典中鍵)'我估計是最好看看。 – RIAstar

回答

2
  1. 你也可以寫if (!dictionary[key]) ...

  2. 可以抵消的,而不是循環直通刪除所有鍵的字典對象:dictionary = null;

  3. 我寫了一個廣播級的,你是絕對歡迎如果你願意,可以使用它!它對非顯示對象之間的全球通信非常有效。但是,如果您想允許顯示對象之間進行全局通信,則可以通過舞臺添加和派發自定義事件 - 當然,假設它們已添加到舞臺上。

Broadcast.as

package com.mattie.events.broadcaster 
{ 
//Class 
public class Broadcast 
    { 
    //Variables 
    public var name:String; 
    public var data:Object; 

    //Constructor 
    public function Broadcast(name:String, data:Object) 
     { 
     this.name = name; 
     this.data = data; 
     } 
    } 
} 

Broadcaster.as

package com.mattie.events.broadcaster 
{ 
//Imports 
import flash.utils.Dictionary; 

//Class 
public final class Broadcaster 
    { 
    //Properties 
    private static var singleton:Broadcaster; 

    private var publicationsProperty:Dictionary; 
    private var subscriptionsProperty:Array; 

    //Constructor 
    public function Broadcaster() 
     { 
     if (singleton) 
      throw new Error("Broadcaster is a singleton that cannot be publically instantiated and is only accessible thru the \"broadcaster\" public property."); 

     publicationsProperty = new Dictionary(true); 
     subscriptionsProperty = new Array(); 
     } 

    //Publish Data 
    public function publish(name:String, data:Object = null):void 
     { 
     publicationsProperty[name] = data; 

     for (var i:uint = 0; i < subscriptionsProperty.length; i++) 
      if (subscriptionsProperty[i].name == name) 
       { 
       var handler:Function = subscriptionsProperty[i].handler; 
       handler(new Broadcast(name, data)); 
       } 
     } 

    //Subscribe Handler 
    public function subscribe(name:String, handler:Function):void 
     { 
     if (publicationsProperty[name]) 
      handler(new Broadcast(name, publicationsProperty[name])); 

     for (var i:uint = 0; i < subscriptionsProperty.length; i++) 
      if (subscriptionsProperty[i].name == name && subscriptionsProperty[i].handler == handler) 
       return; 

     subscriptionsProperty.push({name: name, handler: handler}); 
     } 

    //Unpublish Data 
    public function unpublish(name:String):void 
     { 
     delete publicationsProperty[name]; 
     } 

    //Unsubscribe Handler 
    public function unsubscribe(name:String, handler:Function):void 
     { 
     for (var i:uint = 0; i < subscriptionsProperty.length; i++) 
      if (subscriptionsProperty[i].name == name && subscriptionsProperty[i].handler == handler) 
       { 
       subscriptionsProperty.splice(i, 1); 
       return; 
       } 
     } 

    //Publications Getter 
    public function get publications():Dictionary 
     { 
     return publicationsProperty; 
     } 

    //Subscriptions Getter 
    public function get subscriptions():Array 
     { 
     return subscriptionsProperty; 
     } 

    //Singleton Getter 
    public static function get broadcaster():Broadcaster 
     { 
     if (!singleton) 
      singleton = new Broadcaster(); 

     return singleton; 
     } 
    } 
} 
+5

'if(!dictionary [key])' - 如果'dictionary [key] = false'或'dictionary [key] = 0',這很容易出錯。 –

+0

啊,好抓。你是絕對正確的。 – TheDarkIn1978

7

- 你有兩個有效的選項:與undefined比較或與null比較。所不同的是這樣的:

  • undefined:值完全不
  • null存在:存在一個值,但包含空

所以,你選擇什麼在你的情況下,適當的。看例子。

import flash.utils.Dictionary; 

var test:Dictionary = new Dictionary(); 

trace(test[1] == null); // true, because null is internally converted to undefined 
trace(test[1] === null); // false, because of strictly typed comparison 
trace(test[1] == undefined); // true 
trace(test[1] === undefined); // true 

- 我總是在字典中循環,以清除它們時,我有沒有引用(而不是僅僅ptimitive類型,如數字或字符串)。那麼,它不應該是必要的,但這樣我有點幫助垃圾收集器,這通常是一個好主意。

- 這一個讓我感到困惑。你爲什麼需要這種廣播?它看起來很像我們之前在AS1-2中使用AsBroadcaster類的東西,它非本地地爲我們提供了廣播功能。AS3有一個本地事件調度系統,您可以升級以適應您的需求(例如,如果您需要維護每個事件類型的偵聽器列表)。

這些鏈接可能是有用的:

+0

您也可以使用「in」表達式,例如'如果(鍵入字典){...}' –