1

我正在构建一个 Adob​​e AIR 应用程序,并将字典存储在我的 SharedObject 中。类似于这里提到的内容。

但是为什么我的字典的长度总是空的。我最终不得不手动跟踪似乎有效但不能让我充满信心的长度。

另外,有人可以解释何时调用 readExternal 和 writeExternal 吗?我发现它们被多次调用(因为在我看来是不必要的)。因为它是一本字典,所以多次添加相同的值并不是问题,但再次想知道我是否在这里设置了错误来触发它。

这就是我有一个

public class SharedObjectDictionary implements IExternalizable {

private var _dictionary:Dictionary;
private var _length:int;

public function addItem and removeItem
{
... just adds/removes to my _dictionary ...
... and keeps track of the length ...
}

public function readExternal(input:IDataInput):void
{
    var count:int = input.readInt();
    for (var i:Number = 0;i<count;i++)
    {
        var key:String = input.readObject();
        var val:String = input.readObject();

        addItem(key,val);
    }
}

public function writeExternal(output:IDataOutput):void
{
    output.writeInt(_length);

    for (var key:Object in dictionary)
    {
        output.writeObject(key);
        output.writeObject(dictionary[key]);
    }
}

}

当我调用存储此 SharedObject 时

    var so:SharedObject = SharedObject.getLocal("xxxx");
    so.data["xxxxx"] = instance of the SharedObjectDictionary above;
    so.flush();
    so.close();
    so = null;

我发现 readExternal 被调用一次,而 writeExternal 被调用了 3 次。

谢谢!

4

1 回答 1

0

你的Dictionary的长度总是null因为Dictionary没有定义这样的属性。如果您尝试访问某个其他类上不存在的属性,您将收到编译时错误。但是由于Dictionary该类是动态的,因此不会发生此类编译错误。唯一的解决方案是自己跟踪长度。

我无法评论您关于readExternal/ writeExternal... 的其他问题,您有任何可以展示的代码吗?

于 2013-07-12T16:40:39.113 回答