IOS archive and archive, persistent Storage
Click here to download iOS archive, persistent storage, and archive detailed projects
// The essence of Data Persistence: Read data into a file and save it locally. the Sandbox mechanism is the local folder (names are randomly generated) generated by the system for each program. for different applications, the content in the sandbox of other applications cannot be accessed, protects the content of this application: 1. Documents: used to store data that has been stored for a long time. 2. xxx. app: the package of the application, including all the resources required for the application to load (readonly read-only, cannot be modified). The NSBundle used in normal times is the package 3 Library: 1) Caches: local cache, store the data (Videos, Musics, and Images) that you want to temporarily save, for example, the downloaded video, audio, and Images are stored in this folder. 2) Preferences: stores user Preferences, for example, whether the program starts 4 tmp for the first time: stores the video and audio that have not been downloaded. After the download, transfer the file to the # import "WYLReadAndWriteViewController In the Caches folder. h "# import" WYLArchive. h "@ interface WYLReadAndWriteViewController ()
@ End @ implementation dependencies-(id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) dependencies {self = [super initWithNibName: nibNameOrNil bundle: callback]; if (self) {// initim initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. self. view. backgroundColor = [UIColor whiteColor]; self. navigationItem. title = @ "file read/write"; UITextField * textField = [[UITextField alloc] initWithFrame: CGRectMake (40, 84,220, 40)]; textField. tag= 100; textField. placeholder = @ "Enter the content"; textField. delegate = self; textField. borderStyle = UITextBorderStyleRoundedRect; [self. view addSubview: textField]; [textField release]; UITextField * textField2 = [[UITextField alloc] initWithFrame: CGRectMake (40,174,220, 40)]; textField2.tag = 101; textField2.placeholder = @ "show the content of the previous input box"; textField2.delegate = self; textField2.borderStyle = UITextBorderStyleRoundedRect; [self. view addSubview: textField2]; [textField2 release]; UIButton * writeButton = [UIButton buttonWithType: UIButtonTypeSystem]; writeButton. frame = CGRectMake (45,260, 60, 30); [writeButton setTitle: @ "write" forState: UIControlStateNormal]; [writeButton addTarget: self action: @ selector (write :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: writeButton]; UIButton * readButton = [UIButton buttonWithType: UIButtonTypeSystem]; readButton. frame = CGRectMake (190,260, 60, 30); [readButton setTitle: @ "read" forState: UIControlStateNormal]; [readButton addTarget: self action: @ selector (read :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: readButton]; UIButton * push = [UIButton buttonWithType: UIButtonTypeSystem]; push. frame = CGRectMake (120,310, 60, 30); [push setTitle: @ "push" forState: UIControlStateNormal]; [push addTarget: self action: @ selector (push :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: push];}-(NSString *) getFilePath {// used to obtain the path of the specified folder: <# NSSearchPathDirectory directory #>: specified folder; <# NSSearchPathDomainMask domainMask #>: Set the search domain. All our files are stored in the Yonghua domain. <# BOOL expandTilde #>: whether to use the detailed path (absolute path) because this method was initially used in mac OS, but for computer systems, it may store multiple users, so the obtained users may have multiple, therefore, the return value type is an array, but only one user is required for iOS. Therefore, there is only one element in the array/* NSString * documentsPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; // 2) NSString * newFilePath = [documentsPath stringByAppendingPathComponent: @ "aa.txt"]; NSLog (@ "% @", newFilePath ); */NSString * filePath = [delimiter (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * newPath = [filePath stringByAppendingPathComponent: @ "test.txt"]; NSLog (@ "% @", newPath); return newPath;}-(void) read :( UIButton *) button {// The previous content will be overwritten each time the data is written. If you want to keep the previous data, you need to talk about the previous data reading, and then splice the data to be stored, and store the data together./* NSString * newFilePath = [self getFilePath]; NSError * error = nil; NSString * content = [NSString stringWithContentsOfFile: newFilePath encoding: NSUTF8StringEncoding error: & error]; UITextField * tf = (UITextField *) [self. view viewWithTag: 101]; tf. text = content; * // string read from local/* NSString * filePath = [self getFilePath]; NSString * content = [NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: nil]; UITextField * tf = (UITextField *) [self. view viewWithTag: 101]; tf. text = content; * // The NSString * filePath = [self getFilePath] is read from the local file; // NSArray * arr = [NSArray arrayWithContentsOfFile: filePath]; // read NSDictionary * dic = [NSDictionary dictionaryWithContentsOfFile: filePath]; UITextField * tf = (UITextField *) [self. view viewWithTag: 100]; UITextField * tf1 = (UITextField *) [self. view viewWithTag: 101]; tf. text = dic [@ "tf2"]; tf1.text = dic [@ "tf1"];} // currently, only NSString, NSArray, NSDictionary, and NSData are supported, and their subclass. write File: writeToFile :( this is the method called by the object), read the file: each class comes with the method that can create objects according to the path: [class name class WithContentsOfFile]; string: [NSString stringWithContentsOfFile], array: [NSArray dictionary], dictionary: [NSDictionary dictionary], binary stream: [NSData dataWithContentsOfFile] (keep in mind: For an array, a dictionary such as a container class, the internal Member must also be one of the eight main categories that can implement file read and write)-(void) write :( UIButton *) button {// when writing, the text in the first input box will be, write to a local file // 1 get the stored content UITextField * tf = (UITextField *) [self. view viewWithTag: 100]; NSString * content = tf. text; // 2 get the path of the file to be stored // 1) Get the Documents folder path NSString * newFilePath = [self getFilePath]; // 3 store the content to the specified file path // NSError * error = nil; // write the string to the local file // BOOL isSucceed = [content writeToFile: newFilePath atomically: YES encoding: NSUTF8StringEncoding error: & error]; // The array is written to the local file UITextField * tf2 = (UITextField *) [self. view viewWithTag: 101]; NSString * content1 = tf2.text; // NSArray * arr = @ [content, content1]; // BOOL isSucceed = [arr writeToFile: newFilePath atomically: YES]; // dictionary write local file NSDictionary * dic =@{@ "tf1": content, @ "tf2": content1}; BOOL isSucceed = [dic writeToFile: newFilePath atomically: YES]; NSLog (@ "% d", isSucceed);}-(void) push :( UIButton *) button {WYLArchive * archivieVC = [[WYLArchive alloc] init]; [self. navigationController pushViewController: archivieVC animated: YES];}-(BOOL) textFieldShouldReturn :( UITextField *) textField {[textField resignFirstResponder]; return YES;}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} /* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller .} */@ end # import "WYLArchive. h "# import" Person. h "@ interface WYLArchive ()
@ End @ implementation WYLArchive-(id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) handle {self = [super initWithNibName: nibNameOrNil bundle: handle]; if) {// initim initialization} return self;}-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view. self. view. backgroundColor = [UIColor whiteColor]; self. navigationItem. title = @ "archive and archive"; UITextField * textField = [[UITextField alloc] initWithFrame: CGRectMake (40, 84,220, 40)]; textField. tag= 100; textField. placeholder = @ "Enter the content"; textField. borderStyle = UITextBorderStyleRoundedRect; textField. delegate = self; [self. view addSubview: textField]; [textField release]; UITextField * textField2 = [[UITextField alloc] initWithFrame: CGRectMake (40,174,220, 40)]; textField2.tag = 101; textField2.placeholder = @ "show the content of the previous input box"; textField2.borderStyle = UITextBorderStyleRoundedRect; textField2.delegate = self; [self. view addSubview: textField2]; [textField2 release]; UIButton * fileButton = [UIButton buttonWithType: UIButtonTypeSystem]; fileButton. frame = CGRectMake (45,260, 60, 30); [fileButton setTitle: @ "ARCHIVE" forState: UIControlStateNormal]; [fileButton addTarget: self action: @ selector (file :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: fileButton]; UIButton * archiveButton = [UIButton buttonWithType: UIButtonTypeSystem]; archiveButton. frame = CGRectMake (190,260, 60, 30); [archiveButton setTitle: @ "" forState: UIControlStateNormal]; [archiveButton addTarget: self action: @ selector (archive :) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: archiveButton];}-(NSString *) getPath {// obtain the folder path/* NSString * filePath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * newPath = [filePath stringByAppendingPathComponent: @ "archive"]; return newPath; */NSString * path = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; NSString * newPath = [path stringByAppendingPathComponent: @ "archiver"]; return newPath;}-(void) file :( UIButton *) button {// obtain the content of the input box UITextField * tf1 = (UITextField *) [self. view viewWithTag: 100]; UITextField * tf2 = (UITextField *) [self. view viewWithTag: 101]; // encapsulate it into the Person object Person * person = [[Person alloc] initWithName: tf1.text gender: tf2.text age: 18]; // 1 create an archive object NSMutableData * data = [NSMutableData data]; NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] alias: data]; // 2 archive [archiver encodeObject: person forKey: @ "person"]; [person release]; // 3. The Archive ends. After the archive ends, the archive is invalid [archiver finishEncoding]; [archiver release]; // 4 write data to the file [data writeToFile: [self getPath] atomically: YES]; */Person * person = [[Person alloc] initWithName: tf1.text gender: tf2.text age: 18]; NSMutableData * data = [NSMutableData data]; NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] metadata: data]; [archiver encodeObject: person forKey: @ "archiver"]; [person release]; [archiver finishEncoding]; [archiver release]; [data writeToFile: [self getPath] atomically: YES];}-(void) archive :( UIButton *) button {/* // 1 initialize NSMutableData object NSMutableData * data = [NSMutableData dataWithContentsOfFile: [self getPath]; // 2 create an anti-archive object NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data]; // 3 anti-archive Person * person = [unarchiver decodeObjectForKey: @ "person"]; // 4. End the archive [unarchiver finishDecoding]; [unarchiver release]; */NSMutableData * data = [NSMutableData dataWithContentsOfFile: [self getPath]; optional * unarchive = [[financialloc] initForReadingWithData: data]; Person * person = [unarchive decodeObjectForKey: @ "archiver"]; [unarchive finishDecoding]; [unarchive release]; UITextField * tf1 = (UITextField *) [self. view viewWithTag: 100]; UITextField * tf2 = (UITextField *) [self. view viewWithTag: 101]; tf1.text = person. gender; tf2.text = person. name;}-(BOOL) textFieldShouldReturn :( UITextField *) textField {[textField resignFirstResponder]; return YES;}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} /* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareForSegue :( UIStoryboardSegue *) segue sender :( id) sender {// Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller .} * // @ end