AS3 for ios : how to serialize an array and then save it

You need to use flash.net.registerClassAlias.

So if I had a class like this:

package com {
    public class Bob {
        public var myArray:Array;
    }
}

I could do this:

registerClassAlias("com.Bob", Bob);

Then you can save Bob class objects into your shared objects (or rather retrieve them again)

Full example:

//register the class - you only need to do this once in your class
registerClassAlias("com.Bob", Bob);

//Lets save a Bob instance into a shared object
var bob:Bob = new Bob();
bob.myArray = ["blah","blah","blah"];

var bobSO:SharedObject = SharedObject.getLocal("Bob");          
bobSO.data.bob = new Bob();
bobSO.flush();

//now at a later time, let's retrieve it
var bobInstance:Bob;
var bobSO:SharedObject = SharedObject.getLocal("Bob");
if (bobSO.size > 0 && bobSO.data.bob && bobSO.data.bob != undefined) {
    bobInstance = bobSO.data.bob;
}

Keep in mind, that you’ll need to register ALL classes that are used (if you have different classes stored in your array).

Leave a Comment