Adobe AIR - 保存本地数据

时间:2011-09-09 14:31:38

标签: actionscript-3 air local-storage

我想将变量保存到加密本地存储。 这个例子正在运行,但是如何扩展它以保存多个变量?

// To save variable to ELS (EncryptedLocalStore)

    function  saveItems() {
    var m1:String = "my item 1"; 
// HOW TO ADD THESE?:
    // var m2:String = "my item 2"; 
    // var m3:String = "my item 3"; 
    // var m4:String = "my item 4"; 
    //
    var bytes:ByteArray = new ByteArray(); 
    bytes.writeUTFBytes(m1); 
    EncryptedLocalStore.setItem("item1", bytes);
    }

    // Read it from ELS
    function  getItems() {
    var storedValue:ByteArray = EncryptedLocalStore.getItem("item1"); 
    //
    trace(storedValue.readUTFBytes(storedValue.length));
    }

感谢。

1 个答案:

答案 0 :(得分:6)

冲洗并重复您已有的内容,并通过将常用功能移动到自己的方法中来避免重复代码:

private function saveItems():void {
    var m1:String = "my item 1"; 
    var m2:String = "my item 2"; 
    var m3:String = "my item 3"; 
    var m4:String = "my item 4"; 

    saveItem("item1", m1);
    saveItem("item2", m2);
    saveItem("item3", m3);
    saveItem("item4", m4);
}

private function saveItem(key:String, value:String):void
{
      var bytes:ByteArray = new ByteArray(); 
      bytes.writeUTFBytes(value); 
      EncryptedLocalStore.setItem(key, bytes);
}

一些很好的参考资料:

http://www.adobe.com/devnet/air/ajax/articles/encrypted_local_store.html

http://help.adobe.com/en_US/air/reference/html/flash/data/EncryptedLocalStore.html