IOS archives Model objects and ios Model objects.
Archive refers to a form of serialization. Any object specially written for data storage should support archiving. You can archive Model objects to easily write complex objects to files and then read them from them.
As long as each attribute implemented in the class is a scalar or an instance of a class that complies with the NSCoding protocol, you can fully archive the entire object. Most of the Foundation and Cocoa Touch classes comply with the NSCoding protocol, so archiving is not difficult for most classes.
Follow the NSCoding protocol:
NSCoding declares two methods,
-(Void) encodeWithCoder :( NSCoder *) ACO; and-(nullable instancetype) initWithCoder :( NSCoder *) aDecoder;
The first is to encode the object into the archive, and the second is to decode the archive to create an object.
In BIDFourLines:
#import <Foundation/Foundation.h>@interface BIDFourLines : NSObject <NSCoding, NSCopying>@property (copy, nonatomic) NSArray *lines;@end
#import "BIDFourLines.h"static NSString * const kLinesKey = @"kLinesKey";@implementation BIDFourLines #pragma mark - Coding- (id)initWithCoder:(NSCoder *)aDecoder{ self = [super init]; if (self) { self.lines = [aDecoder decodeObjectForKey:kLinesKey]; } return self;}- (void)encodeWithCoder:(NSCoder *)aCoder;{ [aCoder encodeObject:self.lines forKey:kLinesKey];}#pragma mark - Copying- (id)copyWithZone:(NSZone *)zone;{ BIDFourLines *copy = [[[self class] allocWithZone:zone] init]; NSMutableArray *linesCopy = [NSMutableArray array]; for (id line in self.lines) { [linesCopy addObject:[line copyWithZone:zone]]; } copy.lines = linesCopy; return copy;}@end
Note: The above follows the NSCopying protocol, which is good for any model object. In the NSCopying protocol, there is a copyWithZone method that can be used to copy objects.
In BIDViewController:
# Import "BIDViewController. h "# import" BIDFourLines. h "static NSString * const kRootKey = @" kRootKey "; @ interface BIDViewController () @ property (strong, nonatomic) IBOutletCollection (UITextField) NSArray * lineFields; @ end @ implementation BIDViewController-(NSString *) dataFilePath {NSArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex: 0]; return [documentsDirectory stringByAppendingPathComponent: @ "data. archive "];}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSString * filePath = [self dataFilePath]; // if an archive if ([[NSFileManager defaultManager] fileExistsAtPath: filePath]) {NSData * data = [[NSMutableData alloc] initWithContentsOfFile: filePath]; optional * unarchiver = [[initalloc] initForReadingWithData: data]; // solution: BIDFourLines * fourLines = [unarchiver decodeObjectForKey: kRootKey]; [unarchiver finishDecoding]; for (int I = 0; I <4; I ++) {UITextField * theField = self. lineFields [I]; theField. text = fourLines. lines [I] ;}}// background notification UIApplication * app = [UIApplication sharedApplication]; [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (applicationWillResignActive :) name: Your object: app];} // archive when entering the background-(void) applicationWillResignActive :( NSNotification *) notification {NSString * filePath = [self dataFilePath]; BIDFourLines * fourLines = [[BIDFourLines alloc] init]; fourLines. lines = [self. lineFields valueForKey: @ "text"]; NSMutableData * data = [[NSMutableData alloc] init]; optional * archiver = [[NSKeyedArchiver alloc] encoding: data]; [archiver encodeObject: fourLines forKey: kRootKey]; [archiver finishEncoding]; [data writeToFile: filePath atomically: YES];}