Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
912 views
in Technique[技术] by (71.8m points)

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

I have an multidimentional array and I want to serialize it and then save it using SharedObjects. I tried many things, but each time there is a problem.

JSON : can't have types with it.

ByteArray : the result is not a String, then the SharedObject doesn't want to save it.

May be SharedObjects are not part of the solution.

So my question : is there a simple solution to save an array on disc and to retrieve this array ?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...