UI sandbox mechanism file read and write operations, ui sandbox
Sandbox mechanism:
The essence of Data Persistence: Reading data into files and storing it locally
Sandbox mechanism: the sandbox is the local folder (names are randomly generated) generated by the system for each application ). different applications cannot access the content in the sandbox of other applications. this protects the application content.
Folder in the sandbox:
(1) Documents: used to store long-lived data
(2) XXX. app: the package of the application, which contains all the resources required for loading the application (readonly is read-only and cannot be modified). The NSBundle used in normal times is the package.
(3) Library:
A: Caches: The local cache stores the data you want to temporarily save. For example, the downloaded video, audio, and Images are all stored in this folder (Videos, Musics, Images ).
B: tmp: stores the video and audio that have not been downloaded. After the download is complete, transfer the file to the Caches folder.
File read/write:
Currently, only NSString, NSArray, NSDictionary, NSData, and their subclasses are supported.
Write File: writeToFile :( this is the method called by the object)
File Reading: each class comes with a method for creating Objects Based on the file path: [class name class WithContentsOfFile:];
String: [NSString stringWithContentsOfFile:];
Dictionary: [NSDictionary dictionaryWithOfFile:];
Binary stream: [NSData dataWithContentsOfFile:];
(Keep in mind: For an array, a dictionary container class, internal members must also be one of the eight classes that can read and write files.
// Obtain the file path
-(NSString *) getFilePath
{
// (1) obtain the Documents folder path
/**
* Used to obtain the path of a specified folder
*
* @ Param NSDocumentDirectory: Specifies the folder (whether it is Documents or Library ).
* @ Param NSUserDomainMask: Set the domain to be searched. All our files are stored in the user domain.
* @ Param YES whether to use the detailed path (absolute path)
*
* @ Return because this method was initially used for mac OS, but for computer systems, it may be stored in multiple users, so there may be multiple paths, therefore, the return value type is array, but for iOS, there is only one user, so there is only one element in the array.
*/
NSString * documentsPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
// (2) path of the file to be stored
NSString * newFilePath = [documentsPath stringByAppendingPathComponent: @ "Tsummer.txt"];
NSLog (@ "% @", newFilePath );
Return newFilePath;
}
// Write operation
-(Void) writeClick :( UIButton *) writeBtn
{
// 1. Get the stored content
UITextField * tf = (UITextField *) [self. viewviewWithTag: 100];
NSString * content = tf. text;
UITextField * tf1 = (UITextField *) [self. viewviewWithTag: 101];
NSString * content1 = tf1.text;
// 2. Obtain the path of the file to be stored
NSString * newFilePath = [selfgetFilePath];
// 3. Store the content to the specified file path
// (1) write the string to a local file
// NSError * error = nil;
// BOOL isSuccess = [content writeToFile: newFilePath atomically: YES encoding: NSUTF8StringEncoding error: & error];
// NSLog (@ "% d", isSuccess );
// (2) write the array to a local file
// NSArray * arr = @ [content, content1];
// BOOL isSuccess = [arr writeToFile: newFilePath atomically: YES];
// NSLog (@ "% d", isSuccess );
// (3) write the dictionary to a local file
NSDictionary * dic =@{@ "tf1": content, @ "tf2": content1 };
BOOL isSuccess = [dic writeToFile: newFilePath atomically: YES];
NSLog (@ "% d", isSuccess );
}
// Read operation
-(Void) readClick :( UIButton *) readBtn
{
// Each write operation overwrites the previous content.
// If you want to retain the previous data, you need to read the previous data first, then splice it with the data to be stored, and write it in one piece.
NSString * newFilePath = [selfgetFilePath];
// String read from local file
// NSString * content = [NSString stringWithContentsOfFile: newFilePath encoding: NSUTF8StringEncoding error: nil];
// (2) read the array from a local file
// NSArray * arr = [NSArray arrayWithContentsOfFile: newFilePath];
// (3) read the dictionary from a local file
NSDictionary * dic = [NSDictionarydictionaryWithContentsOfFile: newFilePath];
UITextField * tf = (UITextField *) [self. viewviewWithTag: 100];
Tf. text = dic [@ "tf1"];
UITextField * tf1 = (UITextField *) [self. viewviewWithTag: 101];
Tf1.text = dic [@ "tf2"];
}
Files contained in the sandbox
By default, each sandbox contains three folders: Documents, Library, and tmp.
Because of the sandbox mechanism of the application, the application can only read and write files in several directories:
Documents: Apple recommends that you save the file data created or browsed in the program to this directory. This directory Library will be included during iTunes backup and recovery: the default setting of the storage program or other status information;
Library/Caches: stores cached files. iTunes will not back up this directory, and files under this directory will not be deleted after the application exits.
Tmp: provides a place to create temporary files in real time.
During synchronization with the iPhone, iTunes backs up all Documents and Library files.
When the iPhone restarts, It discards all tmp files.
.
C ++ windows programming, design a sandbox for controlling file read/write
Hello, I will answer for you:
WINDOWS core programming WINDOWS Programming