Archive for iOS data storage (nskeyedarchiver)/Solution Files (nskeyedunarchiver)

Source: Internet
Author: User

Objective:

The day before yesterday to learn nsuserdefaults, we know nsuserdefaults can not save the custom object, so we come today to meet the archive and reconciliation file. Let's start by reviewing how Java is implemented to save a custom Object! First a custom object must implement the Serializable interface, and then serialize an object into binary data to write to a byte[] data or file, and vice versa to read binary data from a binary data or file into an object, So I think the archive file of iOS is just like the process of the serialization of Java, write a program to try it out.

Test the underlying type first:

 //ArchiveNSString *path = [Nssearchpathfordirectoriesindomains (nsdocumentdirectory, Nsuserdomainmask, YES) ObjectAtIndex:0]; //String DataNSString *string=@"Who is LCJ?"; NSString*filename = [Path stringbyappendingpathcomponent:@"teststring"]; [Nskeyedarchiver archiverootobject:stringTofile:filename]; NSString*string1=[Nskeyedunarchiver Unarchiveobjectwithfile:filename]; NSLog (@"teststring---%@", string1); //Collection ArrayNsmutablearray *array = [Nsmutablearray arraywithobjects:@"W.H.O.",@"ISLCJ", nil]; FileName= [Path stringbyappendingpathcomponent:@"Testarray"];    [Nskeyedarchiver Archiverootobject:array Tofile:filename]; Nsarray*arr =[Nskeyedunarchiver Unarchiveobjectwithfile:filename]; NSLog (@"Testarray---%@", arr); //Dictionary Savefilename = [Path stringbyappendingpathcomponent:@"testnsdictionary"]; Nsdictionary*dictionary=[nsdictionary Dictionarywithobject:@"WHOISLCJ"Forkey:@"name"];    [Nskeyedarchiver archiverootobject:dictionary Tofile:filename]; Nsdictionary*dictionary1=[Nskeyedunarchiver Unarchiveobjectwithfile:filename]; NSLog (@"Dictionary---%@", Dictionary1);

It has been said that Java to achieve serialization deserialization must implement the Serializable interface, then iOS? iOS needs to implement the Nscoding protocol,

Person.h

#import <Foundation/Foundation.h>@interface person:nsobject<nscoding>* name; @end

Person.m

#import "Person.h"@implementation Person#pragmaMark-nscoding delegate-(void) Encodewithcoder: (Nscoder *) Acoder {[Acoder encodeObject:self.name forkey:@"name"];}- (ID) Initwithcoder: (Nscoder *) Adecoder {self.name= [Adecoder Decodeobjectforkey:@"name"]; returnSelf ;}@end

Specific implementation:

    // custom Object Types    Person *person=[[Person alloc]init];    Person.name=@ "whoislcj";     = [Path stringbyappendingpathcomponent:@ "testobject"];    [Nskeyedarchiver Archiverootobject:person tofile:filename];     *tempperson = [Nskeyedunarchiver unarchiveobjectwithfile:filename];    NSLog (@ "tempperson:%@", Tempperson.name);

Archive how to delete

      // Delete an archive file        Nsfilemanager *defaultmanager = [Nsfilemanager Defaultmanager];         if ([Defaultmanager isdeletablefileatpath:filename]) {            [Defaultmanager removeitematpath:filename error:nil];        } 

Cons: Archive the form to save data, only one-time archive save and one-time decompression. So only a small amount of data, and the data operation is clumsy, that is, if you want to change a small part of the data, still need to extract the entire data or archive the entire data.

Archive for iOS data storage (nskeyedarchiver)/Solution Files (nskeyedunarchiver)

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.