Objects in obj-C include nsarray, nsdictionary, nsstring, nsnumber, nsdate, nsdata, and their variable versions (nsmutablearray, nsmutabledictionary... in this way, you can easily serialize your data in a certain format (such as XML format) and save the local files. Example Code : Nsarraytest. h
# Import <Foundation/Foundation. h> # define file_name @ "/tmp/data.txt" @ interface nsarraytest: nsobject {}-(void) test; @ end
Nsarraytest. m
# Import "nsarraytest. H "@ implementation nsarraytest-(void) test {nsarray * arr = [nsarray arraywithobjects: @" one ", @" two ", @" three ", nil]; // note: the last one should end with nil [arr writetofile: file_name atomically: Yes]; // (after being serialized into XML format) save the file nsarray * arr2 = [nsarray arraywithcontentsoffile: file_name]; // read filenslog (@ "% @", arr2);} @ end
Running result: 14:20:01. 501 plist [1246: a0f] (
One,
Two,
Three
) If you view/tmp/data.txt, you can see the following content:
<? XML version = "1.0" encoding = "UTF-8"?> <! Doctype plist public "-// Apple // DTD plist 1.0 // en" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version = "1.0"> <array> <string> one </string> <string> two </string> <string> three </string> </array> </plist>
Nsarray serializes objects in XML format by default. If the classes you use to store data are defined by yourself and are not the preset objects above, you must useFormal protocol nscodingTo achieve serialization and deserialization.
For example, we have a class named sample. h.
# Import <Foundation/Foundation. h >@interface sample: nsobject <nscoding> {nsstring * Name; int magicnumber; float shoesize; nsmutablearray * subthingies;} @ property (copy) nsstring * Name; @ property int magicnumber; @ property float shoesize; @ property (retain) nsmutablearray * subthingies;-(ID) initwithname :( nsstring *) n magicnumber :( INT) m shoesize :( float) ss; @ end
Here we define several different types of attributes, including strings, integers, floating-point numbers, and an array object sample. m with variable length.
# Import "sample. H "@ implementation sample @ synthesize name; @ synthesize magicnumber; @ synthesize shoesize; @ synthesize subthingies;-(ID) initwithname :( nsstring *) n magicnumber :( INT) m shoesize :( float) SS {If (Self = [Super init]) {self. name = N; self. magicnumber = m; self. shoesize = SS; self. subthingies = [nsmutablearray array];} return (Self);}-(void) dealloc {[Name Release]; [subthingies release]; [Super dealloc];} // encode the object (I .e.: serialization)-(void) encodewithcoder :( nscoder *) acder {[acder encodeobject: Name forkey: @ "name"]; [ACO encodeint: magicnumber forkey: @ "magicnumber"]; [ecoderencodefloat: shoesize forkey: @ "shoesize"]; [ecoderencodeobject: subthingies forkey: @ "subthingies"];} // decode the object (deserialization)-(ID) initwithcoder :( nscoder *) adecoder {If (Self = [Super init]) {self. name = [adecoder decodeobjectforkey: @ "name"]; self. magicnumber = [adecoder decodeintforkey: @ "magicnumber"]; self. shoesize = [adecoder decodefloatforkey: @ "shoesize"]; self. subthingies = [adecoder decodeobjectforkey: @ "subthingies"];} return (Self) ;}- (nsstring *) Description {nsstring * description = [nsstring stringwithformat: @ "% @: % d/%. 1f % @ ", name, magicnumber, shoesize, subthingies]; Return (description) ;}@ end
Note: Encodewithcoder And Initwithcoder This is the two methods defined in the nscoding Protocol to implement object encoding and decoding. The implementation is not complex, and the classic hash structure of key-value is used. Of course, in encoding, we recommend that you use define to define the key name string in advance as a constant to avoid incorrect string typing. Test:
# Import <Foundation/Foundation. h> # import "sample. H "int main (INT argc, const char * argv []) {ngutoreleasepool * Pool = [[ngutoreleasepool alloc] init]; sample * S1 = [[sample alloc] initwithname: @ "thing1" magicnumber: 42 shoesize: 10.5]; [s1.subthingies addobject: @ "1"]; [s1.subthingies addobject: @ "2"]; nslog (@ "% @", s1); nsdata * data1 = [nskeyedarchiver archiveddatawithrootobject: S1]; // serialize S1 and save it to nsdata [S1 release]; [data1 writetofile: @ "/tmp/data.txt" atomically: Yes]; // persistently save it as a physical file nsdata * data2 = [nsdata datawithcontentsoffile: @ "/tmp/data.txt"]; // read the file sample * S2 = [nskeyedunarchiver unarchiveobjectwithdata: data2]; // deserialize nslog (@ "% @", S2); [pool drain]; return 0 ;}
Running result:
14:36:48. 540 plist [1322: a0f] thing1: 42/10. 5 (
1,
2
)
14:36:48. 548 plist [1322: a0f] thing1: 42/10. 5 (
1,
2
)
View/tmp/data.txt and the following content:
The content is not as readable as nsarray is.