2011-03-08 62 views
0

我在ActionScript 3類的類成員,看起來是這樣的:如何在ActionScript 3中將矢量公開爲可枚舉類型?

private var m_myvect :Vector.<MyType> = new <MyType>[]; 

我露出它通過一個getter,像這樣:

public function get mydata() :Vector.<MyType> 

這不利於當我用這樣的:

for (var o:Object in inst.mydata) 
{ 
    ... 
} 

...因爲o的類型不是MyType,但而不是,因爲它枚舉了矢量索引而不是數據。

我將如何暴露的方式只在矢量中的數據:

  • 允許我使用for(a in b)語法
  • 不的方式允許它進行修改公開數據?

回答

3
for(var i:int = 0; i < mydata.length; i++) 
    // this will loop using the index - get your object using mydata[i] - it will be typed 

for (var o:Object (or String) in mydata) 
    // this will loop through using the keys - it's more for looping through Objects and Dictionaries 

for each(var type:MyType in mydata) 
    // this will loop through using the values - what you're looking for 

以暴露數據,從被修改停止它(即加法/來自Vector刪除項目,或在MyType的改變的數據)的唯一方法是做到這一點通過父對象上的功能(即持有向量)。

例如

public function changeSomething(index:int, item:*):void 
{ 
    // make sure the index is in bounds 
    if(index < 0 || index >= this.m_myvect.length) 
     return; 
    this.m_myvect[i].whatever = item; 
} 
+0

謝謝..這是'每個'我對'每個'失蹤。 – izb 2011-03-08 12:14:18