2010-09-24 59 views
2

假设我有一个ActionScript类:MyClass并且该类中包含数据。现在,让我们说,我想,使用数据“为每个”遍历:在ActionScript中与“for each”很好地搭配使用?

var myData:MyClass = new MyClass(); 
myData.Populate(fromSource); 

for each(var item in myData) { 
    DoSomethingWith(item); 
} 

当然,这不执行任何操作,因为MyClass是一个自定义类,而我没有做什么特别的呢。

对于MyClass,我需要做些什么才能使它与“for each”很好地搭配?我可以递交一个迭代器或枚举器或其他东西吗?

回答

3

我相信您需要扩展Proxy类并实现nextValue(index:int)。它由for each使用。

+0

+1是的!这让我得到了正确的答案。查看我的答案获得完整的解决方案。 – 2010-09-24 14:37:35

-1

您应该查看for each ... in上的Adobe livedocs页面。他们在那里有你的答案。

[for each ... in]迭代集合中的项目并执行每个项目的语句。作为E4X语言扩展的一部分引入,for each..in语句不仅可用于对于XML对象,对于对象和数组,对于each..in语句只能通过对象的动态属性进行迭代,而不是对固定属性进行迭代。固定属性是一个定义为类定义一部分的属性。使用for each..in语句和一个用户定义类的实例,您必须声明具有动态属性的类。

+0

我不认为这回答我的问题。我不想迭代我的属性,我想迭代我的类中的数据,这可能会有数千个基于懒惰函数的项目。仅仅声明具有动态属性的类是不够的。 – 2010-09-24 13:46:44

2

好吧,我想通了。

@alxx帮我找到了答案。这里是一个完整的答案:

public class MyClass extends Proxy 
{ 
    override flash_proxy function nextNameIndex (index:int):int { 
     // This is the command to move to the next item or start over (index == 0) 
     // return an incremented index when there is data 
     // return 0 when you are done. 
    } 

    override flash_proxy function nextValue(index:int):* { 
     // This is the command to get the data 
     // The index that is passed in is the index returned in nextNameIndex 
    } 
} 
相关问题