2011-12-17 46 views
2

有什麼方法可以在Flex應用程序中使用ActionScript實現字典。例如,我想存儲這樣的東西。Flex/ActionScript中的字典

public var orientation:ArrayCollection = new ArrayCollection([ 
    {a: new Direction('0','0','b','0')}, 
    {b: new Direction('a','c','0','h')}, 
    {c: new Direction('0','b','d','e')}, 
    {d: new Direction('c','0','0','0')}, 
    {e: new Direction('f','g','0','c')}, 
    {f: new Direction('0','0','e','0')}, 
    {g: new Direction('0','0','0','e')}, 
    {h: new Direction('0','b','0','i')}, 
    {i: new Direction('l','h','j','m')}, 
    {j: new Direction('i','k','0','0')}, 
    {k: new Direction('0','0','0','j')}, 
    {l: new Direction('0','0','i','0')}, 
    {m: new Direction('0','i','0','0')} 
]); 

所以這不是

orientation.getItemAt(3).north 

我能去像

orientation.getItemAt('d')north 

沒有收到以下錯誤

Implicit coercion of a value of type String to an unrelated type int. 

感謝你的幫助

回答

6

這是一個很奇怪的結構,你正在使用。爲什麼不直接使用Dictionary

var orientation:Dictionary = new Dictionary(); 
orientation["a"] = new Direction('0','0','b','0'); 
orientation["b"] = new Direction('a','c','0','h'); 
// etc. 

然後您可以直接通過鍵 - trace(orientation["a"])訪問該值。

如果您確實需要使用當前結構,您可以創建一個自定義函數以按照需要的方式訪問項目。這樣的東西應該工作(未經測試):

function getItemByKey(var array:ArrayCollection, var key:String):* { 
    for (var i:int = 0; i < array.length; i++) { 
     var item:* = array[i]; 
     if (item[key]) return item; 
    } 
    return null; 
} 
+1

嗯,還沒有與AS3最近的工作,但你沒有訪問字典喜歡取向[「一」],而不是orientation.a? – Aesphere 2011-12-17 09:11:05