How nscoding is used---archiving on iOS (how to change content).

Source: Internet
Author: User

In the previous chapter we introduced the archive on iOS and the solution, today we have a little change after the archive, so that the results after the solution is different, this method is similar to nsmutablexxx, can modify the parameters inside, let us take a look at it.

The method involved:

Seektofileoffset: This method is to seek the offset of the method, meaning to find the beginning of the text in the document.

Readdataoflength: This method refers to how long the file is read.

Offsetinfile: It means writing to the first position .

#import "ViewController.h"#definePZ NSLog (@ "----I'm a gorgeous split-line----");@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; NSString*path = [Nshomedirectory () stringByAppendingPathComponent:@"Documents/test.txt"];//NSLog (@ "%@", Path); //1. Create a file[[Nsfilemanager Defaultmanager] Createfileatpath:path contents:nil Attributes:nil]; NSString*str =@"Hello, world!.";        PZ; //2. Write-only way to open the file, Felehandleforwritingatpath is to return a write-only method.Nsfilehandle *writehanle =[Nsfilehandle Filehandleforwritingatpath:path]; //The string in str is stored in UTF8 encoded in data.NSData *data =[str datausingencoding:nsutf8stringencoding]; //Deposit the contents of data in Writehandle .[Writehanle Writedata:data]; //once again, it is added directly to the back of the previous string.[Writehanle Writedata:data];    PZ; //seektofileoffset:0 Jumps to the beginning of the file.[Writehanle Seektofileoffset:0]; //writes the string @ "xxx" to the Writehanle file in UTF8 encoding.[Writehanle writedata:[@"XXX"Datausingencoding:nsutf8stringencoding]]; //Offsetinfile is the number of points written to.NSLog (@"%llu", Writehanle.offsetinfile);    PZ; //4. Close the file[Writehanle CloseFile];    PZ; //use Fileurlwithpath to read the path of the path.Nsurl *url =[Nsurl Fileurlwithpath:path]; //and then pass the URL inside to Readhandle.Nsfilehandle *readhandle =[Nsfilehandle Filehandleforreadingfromurl:url Error:nil]; //Offsetinfile is the number of points written to.NSLog (@"%llu-----", Readhandle.offsetinfile); //Seektofileoffset: Is the offset of the specified file.[Readhandle Seektofileoffset:0]; //Readdataoflength: Reads 10 of the length of the Readhandle document.NSData *data1 = [Readhandle readdataoflength:Ten]; //The binary file data1 is stored in the NSString object str2 with UTF8 encoding.NSString *STR2 =[[NSString alloc]initwithdata:data1 encoding:nsutf8stringencoding]; //Close FileNSLog (@"%@", STR2); [Readhandle CloseFile];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

Results of the output:

 the-Ten- +  +: A:58.407filehandledemo[13118:714260]3 the-Ten- +  +: A:58.407filehandledemo[13118:714260]0----- the-Ten- +  +: A:58.407filehandledemo[13118:714260] Xxxlo, WOR

How nscoding is used---archiving on iOS (how to change content).

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.