2014-09-22 31 views
0

與RemoteClass創建類metateg的Flex - 節省的對象的序列化的ArrayCollection到文件

[RemoteClass] 
public class Result 
{ 
    public function Result(){} 
} 

功能在文件中寫入

public function writeData(object:Object):void 
{ 
    var file:File = File.desktopDirectory.resolvePath("data.txt"); 
    if (file.exists) 
     file.deleteFile();  
    var fileStream:FileStream = new FileStream(); 
    fileStream.open(file, FileMode.WRITE); 
    fileStream.writeObject(object); 
    fileStream.close(); 
} 

功能從文件中讀取

public function readData():Object 
{ 
    var file:File = File.desktopDirectory.resolvePath("data.txt"); 
    if(!file.exists) 
     return 0; 
    var fileStream:FileStream = new FileStream(); 
    fileStream.open(file, FileMode.READ); 
    var obj:Object = fileStream.readObject(); 
    fileStream.close(); 
    return obj; 
} 

功能時,應用程序初始化

public function initApplication():void 
{ 
    writeData(new Result()); // write object to file. 
    var result:Result = readData() as Result; // create Result class object and initializing it data from file 
    var array:ArrayCollection = new ArrayCollection(); // create collection and fill it objects of Result class 
    array.addItem(new Result()); 
    array.addItem(new Result()); 
    array.addItem(new Result()); 
    writeData(array); // writing collection in file 
    var arr:ArrayCollection = readData() as ArrayCollection; // initializing new collecion of collection from file. 
} 

這是問題所在。用對象類型的對象返回集合。 如何獲取Result類型對象的集合?下面

for each (var object:Object in arr) 
{ 
     object = object as Result; 
} 

回答

2

使用registerClassAlias() 糟糕的方式之前,序列化/反序列化

+0

我可以得到結果只有一個類的實例,但不能得到結果對象的數組保藏。 – dkhmarnyi 2014-09-22 12:15:06

+0

嘗試註冊ArrayCollection。據我所知,只有幾種類型可能不需要註冊。 – Ingweland 2014-09-22 12:39:22

+0

非常感謝! RegisterClassAlias()或[RemoteObject] metateg可以工作! – dkhmarnyi 2014-09-22 15:18:05