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.