IOS archives Model objects and ios Model objects.

Source: Internet
Author: User

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];}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.