Archive and archive
1. Archiving
NSArray * array = @ [@ "ARCHIVE", @ "", @ ""];
// 1. Create an archive object
NSMutableData * data = [NSMutableDatadata];
NSKeyedArchiver * archiver = [[NSKeyedArchiveralloc] initForWritingWithMutableData: data];
// 2. Archive
[Archiver encodeObject: primary key: @ "array"];
// 3. the archive is terminated. If the archive is terminated, the archive is invalid.
[Archiver finishEncoding];
[Array release];
[Archiver release];
// 4. Obtain the path of the written file. No file is automatically created.
NSString * documentPath =
[NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString * newFiel = [documentPathstringByAppendingPathComponent: @ "bb.txt"];
// 5. Write data to the file
BOOL isSucceed = [datawriteToFile: [selfgetFiel] atomically: YES];
Ii. Archive operations
// 1. initialize NSMutableData Based on the file path
NSMutableData * Data = [NSMutableDatadataWithContentsOfFile:/Users/lanouhn/Library/Application Support/iPhone Simulator/7.1/Applications/300BEFD5-1F93-47A3-A1B3-936195FAA53F];
// 2. Create an archive class
NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData: Data];
// 3. Archive
NSArray * array = [unarchiverdecodeObjectForKey: @ "array"];
// 4. End the archive
[Unarchiver finishDecoding];
[Unarchiver release];
3. Note
1. If you want to archive a class object, you must enable the class to implement the NSCoding protocol.
2. When the object is a string, array, Dictionary, or set, the semantic attribute of the attribute is declared as copy.
3. When assigning values to instance variables after defining attributes, use the setter method.
Example
@ Interface Person: NSObject
@ Property (nonatomic, copy) NSString * name