您可以将一个对象添加到arraycollection中的一个arraycollection中吗?
问题描述:
我曾尝试将一个对象添加到ArrayCollection中的一个ArrayCollection,但它不工作。我得到错误#1009与以下实施:您可以将一个对象添加到arraycollection中的一个arraycollection中吗?
for (var x:Number = 0; x < identifyArrayCollection.length; x++)
{
identifyArrayCollection[x].speedsArrayCollection.addItem(speedsObj);
}
我可以将speedsObj添加到不在ArrayCollection中的ArrayCollection。
任何帮助,将不胜感激。
感谢,
马克
答
下面的代码添加项目speedObj
到在ArrayCollection
称为identifyArrayCollection
指数x
发现ArrayCollection
。
identifyArrayCollection.getItemAt(x).addItem(speedsObj);
这是你要找的吗?
的代码必须执行以下操作:
identifyArrayCollection[x]
//accesses the item stored in identifyArrayCollection
//with the key of the current value of x
//NOT the item stored at index x
.speedsArrayCollection
//accesses the speedsArrayCollection field of the object
//returned from identifyArrayCollection[x]
.addItem(speedsObj)
//this part is "right", add the item speedsObj to the
//ArrayCollection
答
假设 identifyArrayCollection是含有一些对象和 speedsArrayCollection一个ArrayCollection是包含在定义为对象类型的变量的ArrayCollection identifyArrayCollection
你应该做的:
for (var x:Number = 0; x < identifyArrayCollection.length; x++)
{
identifyArrayCollection.getItemAt(x).speedsArrayCollection.addItem(speedsObj);
}
答
不要忘记任何复合对象都需要首先初始化。 例如(假设初始运行):
有两种方法可以做到这一点:@Sam
for (var x:Number = 0; x < identifyArrayCollection.length; x++)
{
if (!identifyArrayCollection[x]) identifyArrayCollection[x] = new ArrayCollection();
identifyArrayCollection[x].addItem(speedsObj);
}
或使用匿名对象,如果你真的想使用明确的命名约定的捎带 - 但请注意这些是不是编译时间检查(也不是使用阵列存取器的任何东西):
for (var x:Number = 0; x < identifyArrayCollection.length; x++)
{
if (!identifyArrayCollection[x])
{
var o:Object = {};
o.speedsArrayCollection = new ArrayCollection();
identifyArrayCollection[x] = o;
}
identifyArrayCollection[x].speedsArrayCollection.addItem(speedsObj);
}