Archive anti-archive-local sandbox store complex objects

Source: Internet
Author: User
Tags tmp folder

Simple objects such as nsstring,nsnumber,nsarray,nsdictionary can be conveniently stored in local sandbox files, and complex objects, such as properties and methods, require the use of archive anti-archiving methods (serialization and deserialization). stored in a nsdata manner.

Archive anti-archiving, the main sequence of operations is: encoding (enCoding), archive (archiver), decode (decoding), Reverse archive (unarchiver)

1. Encoding and decoding methods, you need to accept the <NSCoding> protocol in the class declaration file, in the implementation file. m to complete the protocol method:

1 /*2 * Model.h accepts nscoding agreement3  */4 5 #import<Foundation/Foundation.h>6 #import<UIKit/UIKit.h>7 8 @interfaceModel:nsobject<nscoding>9@property (nonatomic,strong) NSString *name;Ten @property (nonatomic,assign) Nsinteger num; One  A-(Instancetype) Initwithname: (NSString *) name Num: (nsinteger) num; - @end
1 /*2 * MODEL.M implementation of encoding and decoding protocol method3  */4 5 #import "Model.h"6 7 @implementationModel8-(Instancetype) Initwithname: (NSString *) name Num: (nsinteger) num{9Self =[Super init];Ten     if(self) { OneSelf.name =name; ASelf.num =num; -     } -     returnSelf ; the } -  - //Coding -- (void) Encodewithcoder: (Nscoder *) acoder{ +[Acoder encodeObject:self.name Forkey:@"name"]; -[Acoder encodeobject:@ (self.num) Forkey:@"Num"]; + } A  at //decoding --(Instancetype) Initwithcoder: (Nscoder *) adecoder{ -Self =[Super init]; -     if(self) { -Self.name = [Adecoder decodeobjectforkey:@"name"]; -Self.num = [[Adecoder decodeobjectforkey:@"Num"] IntegerValue]; in     } -     returnSelf ; to } +  - @end

2. Archive-Save complex objects; Reverse Archive-Get objects

1 #import "ViewController.h"2 #import "Model.h"3 4 @interfaceViewcontroller ()5@property (Weak, nonatomic) Iboutlet Uiimageview *image;6@property (Weak, nonatomic) Iboutlet Uitextfield *Nametext;7@property (nonatomic,strong) NSString *path;8 @end9 Ten @implementationViewcontroller One  A- (void) Viewdidload { - [Super Viewdidload]; -      the     //Get sandbox path -Self.path = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) [0] stringByAppendingPathComponent:@"Data.data"]; - } -  + //Archive Save Object --(Ibaction) Save: (UIButton *) Sender { +     //Creating Objects AModel *m = [[Model alloc] Initwithname:@"Test"Num:101]; at      -     //Create nsdata (variable), Nskeyedarchiver (the object that implements the archive) -Nsmutabledata *data =[Nsmutabledata data]; -Nskeyedarchiver *archiver =[[Nskeyedarchiver alloc] initforwritingwithmutabledata:data]; -      -     //object for encoding archiving in[Archiver encodeobject:m Forkey:@"m"]; -      to     //Archive Complete (required) + [Archiver finishencoding]; -      the     //write to the sandbox path file * [Data WriteToFile:self.path atomically:yes]; $ }Panax Notoginseng  - //Anti-archive decoding get objects the-(Ibaction)Get:(UIButton *) Sender { +     //Create nsdata (variable), Nskeyedunarchiver (object to implement anti-archive) ANsmutabledata *data =[Nsmutabledata DataWithContentsOfFile:self.path]; theNskeyedunarchiver *unarchiver =[[Nskeyedunarchiver alloc] initforreadingwithdata:data]; +      -     //Anti-archive decoding get objects $Model *m = [Unarchiver decodeobjectforkey:@"m"]; $      -Self.nameText.text = [NSString stringWithFormat:@"%@%ld", M.name, M.num]; - } the  -- (void) didreceivememorywarning {Wuyi [Super didreceivememorywarning]; the     //Dispose of any resources the can be recreated. - } Wu  - @end

*. Main sandbox path:

Nshomedirectory-----------------------> Sandbox master path nsdocumentdirectory-------------------> Documents folder nslibrarydirectory-------------------->library folder Nscachesdirectory---------------------> Caches folder Nstemporarydirectory------------------>tmp folder

Archive anti-archive-local sandbox store complex objects

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.