"Application Background"
When storing data in a database, if the object is too complex and unnecessary to create a complex table, you can directly convert the entire object into a binary database field, then take it out and restore it.
"Implementation Method"
In PHP, such functionality can be achieved using serialization and deserialization.
In OC, Nskeyedarchiver and nskedunarchiver can be used to achieve the object to binary and binary objects, to achieve conversion, must follow the Nscoding protocol, and implement Encodewithcoder method to convert data to binary, Implement Initwithcoder to implement binary data to restore the class object, OC Common classes, such as arrays, dictionaries, etc. have implemented these content, direct conversion can, if it is the class of their own definition, need to implement manually.
The following is the process of converting a custom person class into binary storage and then reading it to deepen the understanding of this feature.
"Specific Cases"
① defines the Peron class, there are three attributes, such as the following, and note that the Nscoding protocol is to be followed.
#import <Foundation/Foundation.h> @interface person:nsobject <NSCoding> @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) Nsinteger age; @property (nonatomic, assign) double height; @end
② implements encoding and decoding methods, and overrides the description method for easy printing of in-class data.
Note: Select the appropriate data type when encode and decode .
#import "Person.h" @implementation person-(void) Encodewithcoder: (Nscoder *) encoder{ [encoder encodeobject: Self.name forkey:@ "name"]; [Encoder encodeInteger:self.age forkey:@ "age"]; [Encoder encodeDouble:self.height forkey:@ "height"]; } -(ID) Initwithcoder: (Nscoder *) decoder{ if (self = [super init]) { self.name = [Decoder decodeobjectforkey:@] Name "]; Self.age = [Decoder decodeintegerforkey:@ "age"]; Self.height = [Decoder decodedoubleforkey:@ "height"]; } return self; } -(NSString *) description{ return [nsstring stringwithformat:@ "%@%ld%lf", _name,_age,_height]; } @end
③ randomly generates some person objects, uses nskeyedarchiver for binary conversions, and then deposits files,notice that because the object is placed in an array, the object does not need to be actively converted to binary, and this process is done automatically, so be sure to implement the above decode and encode methods。
Nsmutablearray *persons = [Nsmutablearray array];for (int i = 0; i <; i++) {person *p = [[Person alloc] init]; p.name = [NSString stringwithformat:@ "<%05d>", Arc4random_uniform (10000)]; P.age = Arc4random_uniform (+); P.height = 1.5 + arc4random_uniform/100.0; [Persons addobject:p];} [Nskeyedarchiver archiverootobject:persons tofile:@ "/users/soulghost/desktop/persons.data"];
④ read files, use Nskeydunarchiver to restore,When you restore an array, the data in the array is also automatically restored。
Nsmutablearray *persons = [Nskeyedunarchiver unarchiveobjectwithfile:@ "/users/soulghost/desktop/persons.data"]; NSLog (@ "%@", persons);
Because the description method is overridden, the contents of the class are printed as follows:
"<07060> 1.960000", "<05393> 1.960000", "<01438> 1.920000", "<00835> 40 1.710000 ", " <00035> 1.780000 ", " <08394> 1.910000 ", " <00504> 1.580000 ", " <03072> 4 1.900000 ", " <00473> 7 1.800000 ", " <04528> 1.790000 ", " <05930> 3 1.790000 ", " <07390> 2 1.590000 ", " <00820> 1.670000 ", ...
With such a simple case, you can learn the arbitrary object and binary alignment method, which can be used across different languages of the class definition to achieve a unified data storage, can easily implement the database storage of the class.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(115) using Nskeyedarchiver to convert arbitrary objects into binary