如何将对象保存到文件(AS3 / Air)

发布于 2024-12-12 06:50:29 字数 1684 浏览 0 评论 0原文

我想要什么? 将我的项目保存在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甩你一脸翔 2024-12-19 06:50:29

您是否尝试过序列化您的对象?您可以将其序列化为 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文