Archive files (file write and read)

Source: Internet
Author: User

* * First Way: Object Single * *//Archive-(void) nskeyedarchiver{Nsarray*array = [Nsarrayarraywithobjects:@"L",@"J",@"F",Nil];NSString*filepath = [Nshomedirectory () stringbyappendingpathcomponent:@"Array. LJF "];NSLog(@"%@", FilePath);BOOLSuccess = [Nskeyedarchiver Archiverootobject:array Tofile:filepath];if(success) {NSLog(@"saved successfully"); }}//Solve files-(void) nskeyedunarchiver{NSString*filepath = [Nshomedirectory () stringbyappendingpathcomponent:@"Array. LJF "];IDArray = [Nskeyedunarchiver Unarchiveobjectwithfile:filepath];NSLog(@"%@", array);}
* * Second Way: Object Multiple * *//Archive 2-(void) nskeyedarchivertwo{Nsarray*array = [Nsarrayarraywithobjects:@"L",@"J",@"F",Nil];nsdictionary*dic = @{@"Name": array};    Nsmutabledata *data = [Nsmutabledata data]; Nskeyedarchiver *archiver = [[Nskeyedarchiver alloc] initforwritingwithmutabledata:data];//Coding[Archiver encodeobject:dic forkey:@"DIC"]; [Archiver Encodeobject:array forkey:@"Array"]; [Archiver Encodeint: -forkey:@"num"]; [Archiver encodeobject:@"LJF"forkey:@"Name"];//Complete the encoding to populate the above archived data into data, where the archived objects are already stored[Archiver finishencoding];NSString*filepath = [Nshomedirectory () stringbyappendingpathcomponent:@"Array. LJF "];BOOLSuccess = [Data Writetofile:filepath atomically:YES];if(success) {NSLog(@"Archive Success"); }}//Solution 2-(void) nskeyedunarchivertwo{NSString*filepath = [Nshomedirectory () stringbyappendingpathcomponent:@"Array. LJF "];//Read archived dataNSData *data = [[NSData alloc] initwithcontentsoffile:filepath];//Create an Archive object to unpack dataNskeyedunarchiver *unarchiver = [[Nskeyedunarchiver alloc] initforreadingwithdata:data];//Solution archive    Nsarray*array = [Unarchiver decodeobjectforkey:@"Array"];NSLog(@"%@", array);nsdictionary*dic = [Unarchiver decodeobjectforkey:@"DIC"];NSLog(@"%@", DIC);NSString*name = [Unarchiver decodeobjectforkey:@"Name"];NSLog(@"%@", name);intValue = [Unarchiver decodeintegerforkey:@"num"];NSLog(@"%d", value);}

Archive files (file write and read)

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.