There are several methods for reading and writing files in the sandbox, and several methods for reading and writing files in the sandbox.
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
// H obtain the application sandbox
NSString * homaPath = NSHomeDirectory ();
NSLog (@ "% @", homaPath );
}
/**
* Below is how to save data using plist
*/
-(Void) savePlist {
NSString * homePath = NSHomeDirectory ();
NSString * filePath = [homePath stringByAppendingPathComponent: @ "Docments"];
// Specify the file format
NSString * format = [filePath stringByAppendingPathComponent: @ "xx. plist"];
NSArray * data = @ [@ "fang LAN Feng 1", @ "fang LAN Feng 2"];
[Data writeToFile: format atomically: YES];
// The second method is provided by the system.
NSString * dataPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0];
NSString * filePathNssear = [dataPath stringByAppendingPathComponent: @ "xx. plist"];
}
/**
* Plist reads data.
*/
-(Void) readPlist {
// Specify the file
NSString * homePath = NSHomeDirectory ();
NSString * filePath = [homePath stringByAppendingPathComponent: @ "Documents"];
// Obtain data
NSArray * data = [NSArray arrayWithContentsOfFile: filePath];
}
// Preference method ---> preference settings
-(Void) preferenceSave {
NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
[Ud setObject: @ "value11" forKey: @ "key1"];
[Ud setObject: @ "value2" forKey: @ "key2"];
// Synchronization. Make sure to write this step.
[Ud synchronize];
}
/**
* The following is the preference reading method.
*/
-(Void) preferenceRead {
NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
NSLog (@ "% @", [ud objectForKey: @ "key1"]);
}
/**
* Data Storage and reading ==>>>>>> archive and archive
*/
-(Void) keyedArchiverWrite {
NSString * docPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0];
NSString * filePath = [docPath stringByAppendingPathComponent: @ "xx. data"];
Teacher * teacher = [[Teacher alloc] init];
Teacher. name = @ "junk ";
Teacher. age = 10;
// Archive
[NSKeyedArchiver archiveRootObject: teacher toFile: filePath];
}
-(Void) keyedUnarchiverRead {
NSString * filePath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) [0];
NSString * documentType = [filePath stringByAppendingPathComponent: @ "xx. data"];
Teacher * taecher = [NSKeyedUnarchiver unarchiveObjectWithFile: documentType];
}
@ End