如何将对象保存到文件(AS3 / Air)
我想要什么? 将我的项目保存在使用 Air Application 创建的编辑器中
要保存什么? 1 对象 ->类型 ArrayCollection ->包含->来自自己的类的对象...
我的第一次尝试是什么?
var stream :FileStream = new FileStream();
stream.writeObject(myArrayCollection);
问题是什么? myArrayCollection 内部的对象具有以下结构:
public class MyClass1
{
public var title:String;
public var description:String;
public var kindOf:String = "...";
public var thumbnail:String;
public var children:ArrayCollection
...}
每个公共变量都由 FileStream 保存......工作正常 子级 arrayCollection 内部是此类中的对象:
public class MyClass2 extends XMLDocument implements IExternalizable
{
public const kindOf:String = "Seite";
[Bindable]
public var title:String;
public var contenBox:OwnClass; //extended spark Group
public function get childrens():ArrayCollection
{
var childs: ArrayCollection = new ArrayCollection();
var i:int = 0;
while(i > (contenBox.container as spark.components.Group).numElements)
{
childs.addItem((contenBox.container as spark.components.Group).getElementAt(i));
i--;
}
return childs;
}
public function readExternal(input:IDataInput):void
{
trace("hello i'am reading");
}
public function writeExternal(output:IDataOutput):void
{
trace("hello i'am writing");
output.writeObject(children);
}
现在问题... fileStream 包含带有 var“title” 的 MyClass2 对象,但没有子级 ArrayCollection... 控制台不显示痕迹:-/
What I want?
Save my Project in a Editor created with Air Application
What is to save?
1 Object -> Type ArrayCollection -> Contains -> Objects from own classes...
What was my first try?
var stream :FileStream = new FileStream();
stream.writeObject(myArrayCollection);
What was the Problem?
The objects inside of myArrayCollection got this Structure:
public class MyClass1
{
public var title:String;
public var description:String;
public var kindOf:String = "...";
public var thumbnail:String;
public var children:ArrayCollection
...}
Every public var were saved by the FileStream.... works fine
Inside the children arrayCollection are objects from this class:
public class MyClass2 extends XMLDocument implements IExternalizable
{
public const kindOf:String = "Seite";
[Bindable]
public var title:String;
public var contenBox:OwnClass; //extended spark Group
public function get childrens():ArrayCollection
{
var childs: ArrayCollection = new ArrayCollection();
var i:int = 0;
while(i > (contenBox.container as spark.components.Group).numElements)
{
childs.addItem((contenBox.container as spark.components.Group).getElementAt(i));
i--;
}
return childs;
}
public function readExternal(input:IDataInput):void
{
trace("hello i'am reading");
}
public function writeExternal(output:IDataOutput):void
{
trace("hello i'am writing");
output.writeObject(children);
}
Now the Problem... the fileStream contains my MyClass2 objects with the var "title" but there is no children ArrayCollection... the console don't shows the traces :-/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过序列化您的对象?您可以将其序列化为 XML,然后在读取时再次返回。
Flexxb 似乎是一个很好的库,
其他方式可以编写文件作为带有 Bytearray 的原始二进制文件,如所解释的这里
希望有帮助!
Have you tried serializing your Object? You could serialize it to XML and than back again on reading.
It seems a good library for this is Flexxb
other way would to write the file as raw binaries with Bytearray as was explained here
hope it helps!