How to use nscoding---archiving on iOS.

Source: Internet
Author: User

Archiving on iOS is a little different from archiving on Mac, you can put files in any folder, but on iOS, the files you write can only be placed in three folders, documents , and documents. Library, tmp Three files, it is important to note that the library and tmp files will automatically clear the files in the software upgrade, system upgrade or insufficient system space, only in documents can be saved permanently until you delete the software.

Here are the methods involved:

nshomedirectory: This means getting the software's home directory.

stringbyaooendingpathcomponent: The meaning of this method is to add a file after the directory.

Let's take a look at the examples below:

#import "ViewController.h"#definePZ NSLog (@ "----I'm a gorgeous split-line----");@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //Obtain the software's home directory and add the Test.txt file in the documents directory.NSString *path = [Nshomedirectory () stringByAppendingPathComponent:@"Documents/test.txt"]; //the path to the file is created.[[Nsfilemanager Defaultmanager] Createfileatpath:path contents:nil Attributes:nil]; NSString*str =@"Hello, world!."; //2. Open the file in a write-only manner, and assign the file's write path to the Writehanle.Nsfilehandle *writehanle =[Nsfilehandle Filehandleforwritingatpath:path]; //The string in STR is UTF8 encoded into data, where the archive is completed.NSData *data =[str datausingencoding:nsutf8stringencoding]; //Solution: The archived binary code is converted in UTF8 encoded format and assigned to the string object str2.NSString *STR2 =[[NSString Alloc]initwithdata:data encoding:nsutf8stringencoding]; NSLog (@"%@", STR2); //Remember to close the file when you open it, just like memory management.[Writehandle closefile];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

Results of the output:

£ º57.039 filehandledemo[12364:  672084] Hello, world!

How to use nscoding---archiving on iOS.

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.